Why does my Oracle JVM create all these objects for a simple 'Hello World' program?

前端 未结 3 1217
既然无缘
既然无缘 2021-02-04 15:01

I was playing around with jmap and found that simple \"Hello World\" Java program creates thousands of objects. Here is truncated list of objects Oracle JVM

3条回答
  •  -上瘾入骨i
    2021-02-04 15:19

    Checking if tools load additional classes

    I tried the following program:

    package test;
    public class MainSleep {
        public static void main(String[] args) throws InterruptedException {
            synchronized (MainSleep.class) {
                MainSleep.class.wait(5*1000);
            }
        }
    }
    

    When I run it with:

    "c:\Program Files\Java\jdk1.8.0_131\bin\java" \
      -verbose:class -cp target\classes test.MainSleep
    

    I get verbose class loading messages, then a 5 sec pause and then the shutdown does load even more classes:

    ...
    [Loaded sun.misc.PerfCounter from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.misc.Perf$GetPerfAction from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.misc.Perf from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.misc.PerfCounter$CoreCounters from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.nio.ch.DirectBuffer from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.nio.MappedByteBuffer from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.nio.DirectByteBuffer from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.nio.LongBuffer from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.nio.DirectLongBufferU from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.security.PermissionCollection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.security.Permissions from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.net.URLConnection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.net.www.URLConnection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.net.www.protocol.file.FileURLConnection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded sun.net.www.MessageHeader from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.io.FilePermission from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.io.FilePermission$1 from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.io.FilePermissionCollection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.security.AllPermission from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.security.UnresolvedPermission from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.security.BasicPermissionCollection from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded test.MainSleep from file:/D:/ws/BIS65/test-java8/target/classes/]
    [Loaded sun.launcher.LauncherHelper$FXHelper from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.lang.Class$MethodArray from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.lang.Void from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    ...
    [Loaded java.lang.Shutdown from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    [Loaded java.lang.Shutdown$Lock from c:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar]
    

    So this would be the baseline. When I now use jstack or jmap on that file and check for verbose class loading messages I can see if it introduces new classes (not sure about instances of course).

    With jstack -l and jstack, the following additional one class is loaded:

    [Loaded java.lang.Class$MethodArray from
    [Loaded java.lang.Void from
    [Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer ...
    [Loaded java.lang.Shutdown from
    [Loaded java.lang.Shutdown$Lock from
    

    With jstack -F or jstack -m no(!) additional class is loaded:

    [Loaded java.lang.Class$MethodArray from
    [Loaded java.lang.Void from
    [Loaded java.lang.Shutdown from
    [Loaded java.lang.Shutdown$Lock from
    

    None of jmap -clstat,-finalizerinfo,-heap, -histo or -histo:live loaded additional classes:

    [Loaded java.lang.Class$MethodArray from
    [Loaded java.lang.Void from
    [Loaded java.lang.Shutdown from
    [Loaded java.lang.Shutdown$Lock from
    

    The same is true for jmap -dump:format=b,file=ignore.hprof with and without the -F option as well as with and without the live flag.

    Just for completeness, if I use jvisualvm or jconsole it will always trigger a lot of JMX class loads for thread, heap and application snapshots. Most likely because it always opens the dashboard for the process.

    Exploring Heap Content

    So now that we have established this I took a look at the jmap -dump:format=b (non-live, non-forced) heap dump with MAT, looking for the Fields you have been interested in:

    The MAT unreachable objects histogram (which shows instances found in the heap but not connected to any GC root, which is basically all not-yet collected garbage) has 3038 objects, and the top 10:

    Class Name                               | Objects | Shallow Heap
    ------------------------------------------------------------------
    char[]                                   |   1.026 |      113.848
    java.lang.String                         |     599 |       14.376
    int[]                                    |     423 |        7.664
    java.lang.Object[]                       |     220 |       14.192
    java.lang.StringBuilder                  |     137 |        3.288
    java.lang.reflect.Field                  |     115 |        8.280
    java.lang.ProcessEnvironment$CheckedEntry|      66 |        1.056
    java.io.File                             |      59 |        1.888
    java.lang.Class                          |      32 |            0
    java.lang.StringBuffer                   |      30 |          720
    

    There is currently no single live Field instance visible with MAT and only very limited of Class instances. This looks much like a .hprof or MAT problem: the Class instances seems to not show any of their fields in the heap dump. I think they should be softly(!) referenced by Class#reflectionData : SoftReference>, but I think this should be visible in the heap dump and not losing 115 fields. (There is no Class$ReflectionData in the live heap and 14 Class$ReflectionData in the unreachable histo. That can fit well with 115 Fields.

    (I guess I will check back with Serviceability-dev@openjdk on that. This does not fit in a comment, so this is an incomplete answer but I intend to enhance it).

提交回复
热议问题