Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8245617

ObjectSizeCalculator is not supported on OpenJDK

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: openjdk8u262
    • Fix Version/s: None
    • Component/s: other-libs
    • Labels:
      None
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      Problem reported by one of our users:

      import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;

      public class GetObjectSizeDemo {

          public static void main(String[] args) {
              GetObjectSizeDemo demo = new GetObjectSizeDemo();
              try {
                  long size = ObjectSizeCalculator.getObjectSize(demo);
                  System.out.println("size = " + size);
              } catch (Exception e){
                  System.out.println(e.getStackTrace());
              }
          }

      }

      $ java GetObjectSizeDemo

      Exception in thread "main" java.lang.ExceptionInInitializerError
              at jdk.nashorn.internal.ir.debug.ObjectSizeCalculator.getObjectSize(ObjectSizeCalculator.java:122)
              at GetObjectSizeDemo.main(GetObjectSizeDemo.java:8)
      Caused by: java.lang.UnsupportedOperationException: ObjectSizeCalculator only supported on HotSpot VM
              at jdk.nashorn.internal.ir.debug.ObjectSizeCalculator.getEffectiveMemoryLayoutSpecification(ObjectSizeCalculator.java:400)
              at jdk.nashorn.internal.ir.debug.ObjectSizeCalculator$CurrentLayout.<clinit>(ObjectSizeCalculator.java:102)
              ... 2 more

      Trivial fix:

      diff -r f5a3d8f60cf5 src/jdk/nashorn/internal/ir/debug/ObjectSizeCalculator.java
      --- a/src/jdk/nashorn/internal/ir/debug/ObjectSizeCalculator.java Tue May 19 05:11:20 2020 +0100
      +++ b/src/jdk/nashorn/internal/ir/debug/ObjectSizeCalculator.java Fri May 22 15:50:37 2020 +0800
      @@ -396,7 +396,8 @@
            */
           public static MemoryLayoutSpecification getEffectiveMemoryLayoutSpecification() {
               final String vmName = System.getProperty("java.vm.name");
      - if (vmName == null || !vmName.startsWith("Java HotSpot(TM) ")) {
      + if (vmName == null
      + || (!vmName.startsWith("OpenJDK ") && !vmName.startsWith("Java HotSpot(TM) "))) {
                   throw new UnsupportedOperationException(
                           "ObjectSizeCalculator only supported on HotSpot VM");
               }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                fyang Fei Yang
                Reporter:
                fyang Fei Yang
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: