Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With recent Sun JVMs (1.6), is it possible to get GC thread information?

With JRockit, you can get the full list of threads by any means, and all of these means include information about the Garbage Collection Thread(s):

1) Asking the Thread class for the information:

Thread.getAllStackTraces();

2) Using ThreadGroup to get that information:

ThreadGroup root = Thread.currentThread().getThreadGroup();
while (root.getParent() != null) {
    root = root.getParent();
}
Thread[] list = new Thread[root.activeCount() + 5];
root.enumerate(list, true);

3) Using JMX to get the list:

ThreadMXBean THREAD_MX_BEAN = ManagementFactory.getThreadMXBean();
long[] tids = THREAD_MX_BEAN.getAllThreadIds();
ThreadInfo[] tinfos = THREAD_MX_BEAN.getThreadInfo(tids);

4) CTRL-BREAK

However, using the Sun JVM -- at least recent Java 6 releases -- only CTRL-BREAK seems to include the Garbage Collection threads and the VM Periodic Task thread. I find it useful to monitor CPU used by the GC threads so my application can detect and log when GC is using most of the CPU time. Without this information, you only know when GC exceeds certain set thresholds.

If I can even just find out the Thread ID of the GC threads, then JMX will probably give the rest of the information I need (unless there is something different about these Threads). For example, using the method:

long threadId = tids[0];
long cpuTime = THREAD_MX_BEAN.getThreadCpuTime(threadId);

Does anyone know how -- or if it is known to be not possible -- to get information about the garbage collection Thread(s) using the Sun JVM?

like image 244
Eddie Avatar asked Oct 16 '10 02:10

Eddie


People also ask

What happens to the application thread when GC happens?

At a certain point in time, an event happens that triggers garbage collection. To clear the memory, application threads have to be stopped. This is where the work of your application stops and the next steps start. The garbage collector marks objects that are no longer used and reclaims the memory.

Is g1gc available in Java 8?

At this stage, we will focus on the tried and tested G1 GC which is available in Java 8, as well as Java 11 (Supported JDK for most ForgeRock applications).

What is difference between concurrent mark sweep and G1 Garbage Collector?

Thus, with each garbage collection, G1 continuously works to reduce fragmentation. This is beyond the capability of both of the previous methods. CMS (Concurrent Mark Sweep) garbage collection does not do compaction. Parallel compaction performs only whole-heap compaction, which results in considerable pause times.

How can I increase my GC threads?

To modify the number of threads, use the flag-XX:ParallelGCThreads=#, where # is the number of threads (in our case, eight). Once you've enabled those flags, test the application and see how much performance you've gained. Ideally, your application should now run faster and have shorter GC pause times.


2 Answers

This is specific to the Java 1.5+ Sun (HotSpot) JVM. Register the MBean sun.management.HotspotInternal in the MBeanServer you are looking to monitor from. This will trigger the registration of these HotSpot internal mbeans:

  • sun.management:type=HotspotClassLoading
  • sun.management:type=HotspotCompilation
  • sun.management:type=HotspotMemory
  • sun.management:type=HotspotRuntime
  • sun.management:type=HotspotThreading

The HotspotThreading MBean has an attribute called InternalThreadCpuTimes which is a map of HotSpot's internal threads.The GC threads are identifiable by name. For example, in the JVM I am running right now, they are called:

  • GC task thread#1 (ParallelGC)
  • GC task thread#0 (ParallelGC)

The value of the map is the CPU time for each thread.

The HotSpotMemory MBean also has an attribute called InternalMemoryCounters which has a few additional bits of information about GC.

like image 197
Nicholas Avatar answered Sep 19 '22 12:09

Nicholas


A first step is to use verbosegc: java -verbose:gc -XX:+PrintGCDetails, which will give you some information about (wall clock) time consumed in GC operations, and the type of operation (full or incremental). Your question seems to be asking whether you can get it programmatically -- probably can get some info via the management I/F.

Edited to add: Some of this is available via the MemoryMXBean, but not the specifics of GC time. Sorry...

like image 25
andersoj Avatar answered Sep 19 '22 12:09

andersoj