views:

216

answers:

5

I'm looking for a technique to find out the Garbage Collection (GC) strategy (collector) the Java VM is using at a given point of time. (Later on, I'd like it to correctly reflect the strategy that I choose, say XX:+UseConcMarkSweepGC.)

verbose:gc (in its basic form) does not help as it just shows me what all it did with each generation. Is there any other flag I can set to make it spit out the GC strategy being utilized?

JDK version is 1.6_21

A: 

Again, GC behavior can be influenced, but not predicted.

SidCool
I'm interesting in finding out what all it got influenced by. :)
pugmarx
A: 

When you start Java with -XX:+PrintGC, it will print messages whenever it garbage-collects

-XX:+PrintGCDetails It will print details

org.life.java
Umm. That doesn't print the strategy. That's the first thing I had tried.
pugmarx
+3  A: 

Mmmm.. you can certainly find out what strategy is being used by a certain JVM with jconsole (VM Summary) page. Not sure about influencing it, or changing it.

EDIT: To assist you with programmatically checking and changing JVM flags of a running VM you can use the jinfo.exe utility in the JDK. For example to check if the ParallelGC flag is set you can run: jinfo.exe -flag UseParallelGC <PID>.

Strelok
Yay! I think that's the answer to my woes. :)jconsole gave me details of the form:--Garbage collector: Name = 'PS MarkSweep', Collections = 0, Total time spent = 0.000 secondsGarbage collector: Name = 'PS Scavenge', Collections = 9, Total time spent = 0.028 seconds--And yes, it did get influenced when I set the VM args: -XX:+UseParallelGC (and overrode the default (ConcMarkSweepGC)).Thanks!
pugmarx
Ok, thanks for the jinfo part.
pugmarx
Just that, I'm not able to figure the default strategy VM is using (that is, if I don't explicitly pass a VM arg for GC). jconsole summary shows me this:Garbage collector: Name = 'Copy', Collections = 319, Total time spent = 1.188 secondsGarbage collector: Name = 'MarkSweepCompact', Collections = 5, Total time spent = 0.454 secondsBut when I use jinfo.exe -flag UseConcMarkSweepGC <pid>I gives me a negative flag (indicating that its not being used). Anyway, I'll keep digging.
pugmarx
+1  A: 

you can try jinfo utility provided with JDK. Provide the JVM process PID to this utility and it will display you all the generic argument passed to this JVM process. Following is the URL for the same :

http://download.oracle.com/javase/6/docs/technotes/tools/share/jinfo.html

Hope it helps.

Anil Vishnoi
+2  A: 

If you want to get this information at runtime in the application that is running on the JVM, then you can use the GarbageCollectorMXBean:

List<GarbageCollectorMXBean> gcs =
  ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gc : gcs) {
  System.out.println(gc.getName());
}
Christian Vest Hansen