Programmatically print the use of the heap, which is usually printed on the JVM output with the GC protocol enabled

When my Java application (Oracle JVM v1.6.0) turned on the GC log, it usually outputs the following output:

PSYoungGen total 6429824K, used 5483163K [0x0000000640000000, 0x0000000800000000, 0x0000000800000000) eden space 5483456K, 99% used [0x0000000640000000,0x000000078eaa6cc0,0x000000078eaf0000) from space 946368K, 0% used [0x00000007c63d0000,0x00000007c63d0000,0x0000000800000000) to space 910208K, 0% used [0x000000078eaf0000,0x000000078eaf0000,0x00000007c63d0000) ParOldGen total 3145728K, used 3145724K [0x0000000580000000, 0x0000000640000000, 0x0000000640000000) object space 3145728K, 99% used [0x0000000580000000,0x000000063ffff3a0,0x0000000640000000) PSPermGen total 57984K, used 57686K [0x000000057ac00000, 0x000000057e4a0000, 0x0000000580000000) object space 57984K, 99% used [0x000000057ac00000,0x000000057e455ae0,0x000000057e4a0000) 

Is there a way to generate this output periodically during application startup? I know How to use programmatic memory access through JMX? , but honestly, I hope for a shortcut or reimplementation of the code that generates the above. For example, is it possible that sending a kill signal to the JVM will lead to the output of the above output?

+4
source share
2 answers

What happened to MXBeans? The implementation is not so complicated.

I used something like this:

  List<GarbageCollectorMXBean> gcList = ManagementFactory.getGarbageCollectorMXBeans(); for(GarbageCollectorMXBean tmpGC : gcList){ System.out.println("\nName: " + tmpGC.getName()); System.out.println("Collection count: " + tmpGC.getCollectionCount()); System.out.println("Collection time: " + tmpGC.getCollectionTime()); System.out.println("Memory Pools: "); String[] memoryPoolNames = tmpGC.getMemoryPoolNames(); for(String mpnTmp : memoryPoolNames){ System.out.println("\t" + mpnTmp); } } System.out.println( "Memory Pools Info" ); List<MemoryPoolMXBean> memoryList = ManagementFactory.getMemoryPoolMXBeans(); for(MemoryPoolMXBean tmpMem : memoryList){ System.out.println("\nName: " + tmpMem.getName()); System.out.println("Usage: " + tmpMem.getUsage()); System.out.println("Collection Usage: " + tmpMem.getCollectionUsage()); System.out.println("Peak Usage: " + tmpMem.getPeakUsage()); System.out.println("Type: " + tmpMem.getType()); System.out.println("Memory Manager Names: ") ; String[] memManagerNames = tmpMem.getMemoryManagerNames(); for(String mmnTmp : memManagerNames){ System.out.println("\t" + mmnTmp); } System.out.println("\n"); } MemoryUsage mu =ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); MemoryUsage muNH =ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); System.out.println( "Init :"+mu.getInit()+ "\nMax :"+mu.getMax()+ "\nUsed :"+mu.getUsed()+ "\nCommited :"+mu.getCommitted()+ "\nInit NH :"+muNH.getInit()+ "\nMax NH :"+muNH.getMax()+ "\nUsed NH:"+muNH.getUsed()+ "\nCommited NH:"+muNH.getCommitted()); 
+3
source

You can run the JVM with parameters to control the runtime of this information.

See the java tool documentation.

For example, to open the console:

 java -verbose:gc 

or to output to a file:

 java -Xloggc:your.log.file 

Using any of these options, the JVM logs each GC event while the virtual machine is running (not only after it leaves).

+2
source

All Articles