views:

82

answers:

2

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?

+1  A: 

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...

andersoj
Yes, but this doesn't help me *programmatically* get information about GC Threads. But of course the detailed output you refer to can be analyzed after the fact, for sure.
Eddie
With JRockit, you the list of threads you can get by various means includes the GC Threads, and you can then use JMX to get the CPU time consumed by the individual GC Threads. With the Sun JVM, the GC threads don't appear to be included in any thread lists.
Eddie