Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you get basic GC stats in Java?

I would like to have some long-running server applications periodically output general GC performance numbers in Java, something like the GC equivalent of Runtime.freeMemory(), etc. Something like number of cycles done, average time, etc.

We have systems running on customer machines where there is a suspicion that misconfigured memory pools are causing excessive GC frequency and length - it occurs to me that it would be good in general to periodically report the basic GC activity.

Is there any platform independent way to do this?

EDIT: I specifically want to output this data to the system log (the console), while running; this is not something I want to connect to the JVM for, as would be with JConsole or JVisualVM.

Edit2: The MX bean looks like what I want - does anyone have a working code example which obtains one of these?

like image 455
Lawrence Dol Avatar asked Jan 21 '09 20:01

Lawrence Dol


People also ask

Does Java have GC?

Java Memory Management, with its built-in garbage collection, is one of the language's finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse.

What is the default GC in Java?

G1 Garbage Collector is the default garbage collection of Java 9. G1 collector replaced the CMS collector since it's more performance efficient. How G1 Garbage Collector works is different from other collectors. Unlike other collectors, the G1 collector partitions the heap space into multiple equal-sized regions.

What is the default GC in Java 9?

Oracle's Java 9 Hotspot VM ships with the Garbage First (G1) GC as its default garbage collector. This GC, first introduced in Java 7, has the unique ability to efficiently and concurrently deal with very large heaps. It can also be configured to not exceed a maximum pause time.


2 Answers

Here's an example using GarbageCollectorMXBean to print out GC stats. Presumably you would call this method periodically, e.g. scheduling using a ScheduledExecutorService.

public void printGCStats() {     long totalGarbageCollections = 0;     long garbageCollectionTime = 0;      for(GarbageCollectorMXBean gc :             ManagementFactory.getGarbageCollectorMXBeans()) {          long count = gc.getCollectionCount();          if(count >= 0) {             totalGarbageCollections += count;         }          long time = gc.getCollectionTime();          if(time >= 0) {             garbageCollectionTime += time;         }     }      System.out.println("Total Garbage Collections: "         + totalGarbageCollections);     System.out.println("Total Garbage Collection Time (ms): "         + garbageCollectionTime); } 
like image 154
Greg Case Avatar answered Oct 05 '22 16:10

Greg Case


See GarbageCollectorMXBean.

like image 34
Ran Biron Avatar answered Oct 05 '22 16:10

Ran Biron