简体   繁体   中英

Is there any way to print Java runtime memory infomation by adding JVM args

I want to get the heap size committed by JVM and the actual usage (peak value). The application is short-time. Is there any way to get the memory information:

  1. Not using jmap , jcmd , ... (which requires pid)
  2. Not modifying source code (eg. insert: Runtime.getRuntime()...)
  3. Just by passing arguments to JVM java -X:someargs...

It seems that before jdk1.8 java -agentlib:hprof=heap could help, but hprof is removed.

JDK 8

-XX:+PrintGCDetails

The heap layout and usage will be printed at VM exit:

 PSYoungGen      total 443904K, used 283177K [0x00000000dcc00000, 0x00000000f7f00000, 0x0000000100000000)
  eden space 442368K, 63% used [0x00000000dcc00000,0x00000000ee07a6f8,0x00000000f7c00000)
  from space 1536K, 4% used [0x00000000f7d80000,0x00000000f7d90000,0x00000000f7f00000)
  to   space 1536K, 0% used [0x00000000f7c00000,0x00000000f7c00000,0x00000000f7d80000)
 ParOldGen       total 72704K, used 980K [0x0000000096400000, 0x000000009ab00000, 0x00000000dcc00000)
  object space 72704K, 1% used [0x0000000096400000,0x00000000964f5060,0x000000009ab00000)
 Metaspace       used 4568K, capacity 4718K, committed 4992K, reserved 1056768K
  class space    used 472K, capacity 532K, committed 640K, reserved 1048576K

JDK 9+

-Xlog:gc+heap+exit

[9.405s][info][gc,heap,exit]  garbage-first heap   total 276480K, used 149668K [0x0000000700000000, 0x0000000800000000)
[9.405s][info][gc,heap,exit]   region size 1024K, 147 young (150528K), 1 survivors (1024K)
[9.405s][info][gc,heap,exit]  Metaspace       used 6335K, capacity 6395K, committed 6784K, reserved 1056768K
[9.405s][info][gc,heap,exit]   class space    used 511K, capacity 530K, committed 640K, reserved 1048576K

total is the committed memory; the range [0x0000000700000000, 0x0000000800000000) is the reserved space.

If you want to print heap at every GC rather than at VM exit, use -XX:+PrintHeapAtGC in JDK 8, or -Xlog:gc+heap=debug in JDK 9+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM