Search code examples
javajvmjvm-arguments

Within a running JVM, how to programmatically determine the jvm options used at startup?


Background: I'm doing some performance testing on a Java application that gets launched through several layers of indirection, such that I'm not entirely sure that the application is getting launched with the flags that I think it is. I'd like my application to include a sanity check (before it begins its performance test) and include in the results (after the test) information about how the JVM was tuned, such as:

  • Which garbage collector was used?
  • Was/is it actively doing cpu profiling?
  • Was/is it logging gc activity?
  • Was/is it in -Xint or -Xmixed mode?
  • Was/is -XX:ParallelGCThreads set -- if so, to what, and if not, what's the default for this build?
  • Was/is -XX:UseCompressedOops on or off?
  • etc.

Is there any way for Java code to (within a running JVM) query the actual options used for its containing JVM? (Assume that I can't see the command line that launched me, so I can't re-parse those flags.)

If there isn't a general-purpose way to determine this, answers that are specific to a particular JVM implementation are also welcome.

UPDATE:

It's important for the solution to be able to know what the default values are for any value that isn't explicitly supplied on the command-line. Otherwise, it's going to involve a lot of (error-prone) legwork to look up what the default value is for a given combination JVM/platform/version/architecture. I'm testing across a wide variety of JVMs, so I don't want to have to manually figure out what the default setting is for each parameter in each jvm release.


Solution

  • You could use a JMX client (like VisualVM) and then call getVMOption(String name), see HotSpotDiagnosticMXBean.

    Or, if you could pass in at least one set of flags to enable JVM logging, it should be --XX:+LogVMOutput -XX:LogFile=jvm.log and then parse the output of the log from your app. The log contains all the flags/parameters used to startup the JVM.

    Another option is to list the JVM process started by PID with ps -ef and there you can see all the input argument of that process. That should work for any JVM type.