Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Java JVM startup parameters (eg -Xmx)

I'm trying to figure out if there's a way to determine the JVM startup properties from within a running java process. Specifically I'm trying to find out where parameters such as -Xmx (max heap size) and -XX:MaxPermSize are stored. I'm running Sun's 1.6 jvm.

If you're wondering why I want to do this, I have a number of JVM webservers that may or may not be configured correctly and I want to add this to the startup code check. It's much easier for me to check in a piece of java code that gets deployed everywhere than to manually find and check all of the jvm startup files. Right now the jvm configuration files for better or worse are not part of our build process or checked into source control.

like image 493
Bob Albright Avatar asked Oct 05 '09 03:10

Bob Albright


People also ask

What are the and parameters when starting JVM?

1.2 -Xmssize (Minimum Heap Size) Unit indicates the unit of the memory in which we want to specify to the JVM. Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes. For assigning minimum 4GB and maximum 6GB to JVM, we can use following parameters.

How do I get JVM properties?

You can determine the properties and variables of your JVM by determining the process id of java (ps -ef, jps, or task manager), cd'ing to $JAVA_HOME/bin directory, then running jinfo <process id> . Of course you can use grep to find a specific property.

What are the JVM parameters?

Now let's discuss the most frequently used JVM Parameters which are 3 namely as follows: Java Heap Size. Garbage Collector. Print GC.


1 Answers

Try:

import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean;  import java.util.List;  public void runtimeParameters() {   RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();   List<String> aList = bean.getInputArguments();    for (int i = 0; i < aList.size(); i++) {     System.out.println( aList.get( i ) );   } } 

That should show all JVM parameters.

Note: we do not have JVM parameter in VCS either, but in a database, read by our own launchers in productions. That way, we can change those values remotely, without having to redeploy JVM parameter file settings.


You will find a good sumary of various JVM tools to use in this article (from the "Dustin's Software Development Cogitations and Speculations"), including Java Application Launcher links to :

  • ManagementFactory.getRuntimeMXBean() call
  • getInputArguments() javadoc
  • Accessing JVM Arguments from Java (to determine, for instance, if JVM is running in debug mode, in order to alter the "grid initialization" logic of an application)
  • Annotation Type MXBean
  • MXBean Java Tutorial

This technique takes advantage of Platform MXBeans available since J2SE 5 (custom MXBeans support was added in Java SE 6).

Two useful sources of information on the JVM parameters available when using Sun's JVM are:

  • A Collection of JVM Options and
  • Charles Nutter's Favorite Hotspot JVM Flags.

Both of these resources list and describe some/all of the not-recommended-for-the-casual-developer double X arguments (-XX) that are available.

like image 169
VonC Avatar answered Oct 02 '22 11:10

VonC