Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

configuration - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...