public class HelloWorld { public static void main(String []args) { System.out.println('Hello World'); } }
接着使用 javap -c HelloWorld.class 就可以反编译得到如下结果:
Compiled from 'HelloWorld.java' public class HelloWorld { public HelloWorld(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object.'<init>':()V 4: return
public static void main(java.lang.String[]); Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String Hello World 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return }
[root@wupx ~]# jcmd 8825 GC.heap_info 8825: PSYoungGen total 628736K, used 41772K [0x0000000715a00000, 0x0000000746480000, 0x00000007c0000000) eden space 609792K, 4% used [0x0000000715a00000,0x00000007173d5478,0x000000073ad80000) from space 18944K, 80% used [0x000000073ad80000,0x000000073bc75e68,0x000000073c000000) to space 19968K, 0% used [0x0000000745100000,0x0000000745100000,0x0000000746480000) ParOldGen total 250880K, used 21756K [0x00000005c0e00000, 0x00000005d0300000, 0x0000000715a00000) object space 250880K, 8% used [0x00000005c0e00000,0x00000005c233f160,0x00000005d0300000) Metaspace used 44797K, capacity 45562K, committed 45824K, reserved 1089536K class space used 5669K, capacity 5832K, committed 5888K, reserved 1048576K
可以看出可以获取新生代、老年代、元空间、Eden、From Survivor 以及 To Survivor 的大小和占比。
[root@wupx ~]# jmap -heap 8825 Attaching to process ID 8825, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.201-b09
using thread-local object allocation. Parallel GC with 10 thread(s)
[root@wupx ~]# jmap -dump:live,format=b,file=heap.bin 8825 Dumping heap to /root/heap.bin ... Heap dump file created
这个 heap.bin 可以使用 jhat 命令打开,是以 html 的形式展示的。
jhat
jhat 分析 Java 堆的命令,可以将堆中对象以 html 的形式显示出来,支持对象查询语言 OQL,命令内容如下:
现在执行 jhat -port 9999 heap.bin 来将刚刚保存的 heap.bin 以 html 展示出来:
[root@wupx ~]# jhat -port 9999 heap.bin Reading from heap.bin... Dump file created Tue May 12 22:31:55 CST 2020 Snapshot read, resolving... Resolving 570997 objects... Chasing references, expect 114 dots.................................................................................................................. Eliminating duplicate references.................................................................................................................. Snapshot resolved. Started HTTP server on port 9999 Server is ready.
'http-nio-8111-Acceptor-0' #52 daemon prio=5 os_prio=0 tid=0x00007f96c40c5800 nid=0x2653 runnable [0x00007f97c0df9000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) - locked <0x00007f982c6213c8> (a java.lang.Object) at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:455) at java.lang.Thread.run(Thread.java:748)
'http-nio-8111-ClientPoller-0' #50 daemon prio=5 os_prio=0 tid=0x00007f9fc8e7e000 nid=0x2651 runnable [0x00007f97c21fb000] java.lang.Thread.State: RUNNABLE at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method) at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269) at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86) - locked <0x00007f982c622460> (a sun.nio.ch.Util$3) - locked <0x00007f982c622450> (a java.util.Collections$UnmodifiableSet) - locked <0x00007f982c622408> (a sun.nio.ch.EPollSelectorImpl) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97) at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:787) at java.lang.Thread.run(Thread.java:748)