views:

446

answers:

4

I'm still investigating issues I have with GC tuning (see prior question), which involves lots of reading and experimentation.

Sun Java5+ JVMs attempt to automatically select the optimal GC strategy and parameters based on their environment, which is great, but I can't figure out how to query the running JVM to find out what those parameters are.

Ideally, I'd like to see what values of the various GC-related -XX options are being used, as selected automatically by the VM. If I had that, I could have a baseline to begin tweaking.

Anyone know to recover these values from a running VM?

A: 

You can use JMX for that. Kick off JConsole, and it should be displayed under the VM Summary tab. It should display all the arguments that have been passed to the JVM.

To do it programatically, you can refer to another SO answer: How to get vm arguments from inside of java application?

Roopinder
Thanks, but I know what arguments I passed to the VM, because I have the script that runs it. I want to know what values the VM has defaulted to for those parameters I haven't specified.
skaffman
+3  A: 

Check out the HotSpotDiagnosticMBean

The following example will print out the value of an option as well as whether the value is DEFAULT or VM_CREATION:

import java.lang.management.ManagementFactory;

import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;

public class HotSpotTest {

    public static void main(String [] args) throws Exception {
        printHotSpotOption("MaxHeapFreeRatio");
        printHotSpotOption("SurvivorRatio");
        printHotSpotOptions();
    }

    private static void printHotSpotOption(String option) throws Exception {
        ObjectName name = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
        String operationName = "getVMOption";
        Object [] params = new Object [] {option};
        String [] signature = new String[] {String.class.getName()};
        Object result = ManagementFactory.getPlatformMBeanServer().invoke(name, operationName, params, signature);
        CompositeDataSupport data = (CompositeDataSupport) result;

        System.out.println(option);
        System.out.println("- Value: "+data.get("value"));
        System.out.println("- Origin: "+data.get("origin"));
    }

    private static void printHotSpotOptions() throws Exception {
        ObjectName name = new ObjectName("com.sun.management:type=HotSpotDiagnostic");
        String attributeName = "DiagnosticOptions";
        Object result = ManagementFactory.getPlatformMBeanServer().getAttribute(name, attributeName);
        CompositeData [] array = (CompositeData[]) result;
        for (CompositeData d : array) {
            System.out.println(d.get("name"));
            System.out.println("- Value: "+d.get("value"));
            System.out.println("- Origin: "+d.get("origin"));
        }
    }
}
Kevin
+1 Interesting, I hadn't seen that before. I'll have a play, see if it's detailed enough.
skaffman
This is really close to being what I need, with the exception that I need to know which VMOptions to ask for. It's a real shame it can't tell me what's available.
skaffman
I modified the example to print the available diagnostic options. However, it does not print every available VM option. All the VM options are listed here, you might be stuck building a list of the ones you want and querying for them: http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
Kevin
A: 

If you're looking for a quick and easy human-readable tool, jconsole might be your friend here. Specifically, I'm looking at the "VM Summary" tab on my currently running FindBugs process and I see these details:

Current heap size: 788,720 kbytes

Maximum heap size: 932,096 kbytes

Committed memory: 923,648 kbytes

Pending finalization: 0 objects

Garbage collector: Name = 'PS MarkSweep', Collections = 324, Total time spent = 12 minutes

Garbage collector: Name = 'PS Scavenge', Collections = 1,132, Total time spent = 1 minute

Obviously, jvisualvm will give you related details but it doesn't seem to be quite as tightly focused on your specific needs (i.e., quickly readable details on the garbage collector).

Bob Cross
+1  A: 

If you are in a non-Windows environment you can use jmap -heap like explained in this blog entry.

antispam