As part of starting the JVM, how to programmatically determine the jvm parameters used at startup?

Reference Information. I run some performance tests on a Java application that runs through several levels of indirection, so I'm not quite sure if the application starts with flags that I think are. I want my application to enable a health check (before it starts the performance test) and include in the results (after the test) JVM tuning information, for example:

  • Which garbage collector was used?
  • Was he actively doing cpu profiling?
  • Was / was there gc activity recording?
  • Was / is in -Xint or -Xmixed ?
  • Was / is -XX:ParallelGCThreads installed - if so, then what, and if not, what is the default for this assembly?
  • Enabled or disabled / was -XX:UseCompressedOops ?
  • and etc.

Is there any way for Java code (within a running JVM) to query the actual parameters used for its containing JVM? (Suppose I donโ€™t see the command line that launched me, so I canโ€™t see these flags again.)

If there is no universal way to determine this, answers specific to the specific JVM implementation are also welcome.

UPDATE:

It is important that the solution can find out what the default values โ€‹โ€‹are for any value that is not explicitly specified on the command line. Otherwise, it will use a lot of errors (error prone) to find what the default value for this combination is JVM / platform / version / architecture. I am testing a wide variety of JVMs, so I donโ€™t want to manually determine what the default value is for each parameter in every jvm release.

+8
java jvm jvm-arguments
source share
3 answers

You can use the JMX client (e.g. VisualVM) and then call getVMOption(String name) , see HotSpotDiagnosticMXBean .

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

Another option is to list the JVM process launched by the PID using ps -ef , and there you can see all the input arguments to this process. This should work for any type of JVM.

+2
source share

You can get command line arguments

 ManagementFactory.getRuntimeMXBean().getInputArguments(); 
+5
source share

The following Java 7 code will display all the JVM parameters returned by -XX:+PrintFlagsFinal . It tries to use reflection to access the Flag protected helper class (available since Java 6) and returns to HotSpotDiagnosticMXBean.getDiagnosticOptions() if this does not work.

 // load the diagnostic bean first to avoid UnsatisfiedLinkError final HotSpotDiagnosticMXBean hsdiag = ManagementFactory .getPlatformMXBean(HotSpotDiagnosticMXBean.class); List<VMOption> options; try { final Class<?> flagClass = Class.forName("sun.management.Flag"); final Method getAllFlagsMethod = flagClass.getDeclaredMethod("getAllFlags"); final Method getVMOptionMethod = flagClass.getDeclaredMethod("getVMOption"); getAllFlagsMethod.setAccessible(true); getVMOptionMethod.setAccessible(true); final Object result = getAllFlagsMethod.invoke(null); final List<?> flags = (List<?>) result; options = new ArrayList<VMOption>(flags.size()); for (final Object flag : flags) { options.add((VMOption) getVMOptionMethod.invoke(flag)); } } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassCastException e) { if (hsdiag != null) { // only includes writable external flags options = hsdiag.getDiagnosticOptions(); } else { options = Collections.emptyList(); } } final Map<String, VMOption> optionMap = new TreeMap<>(); for (final VMOption option : options) { optionMap.put(option.getName(), option); } for (final VMOption option : optionMap.values()) { System.out.println(option.getName() + " = " + option.getValue() + " (" + option.getOrigin() + ", " + (option.isWriteable() ? "read-write" : "read-only") + ")"); } System.out.println(options.size() + " options found"); 

With 7u71 I get 663 options or with -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions , 779.

+2
source share

All Articles