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.
我试图找出是否有办法从正在运行的java进程中确定JVM启动属性。具体来说,我试图找出存储-Xmx(最大堆大小)和-XX:MaxPermSize等参数的位置。我正在运行Sun的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.
如果你想知道我为什么要这样做,我有许多JVM web服务器可能正确配置,也可能没有配置,我想将它添加到启动代码检查。检查一下到处部署的java代码比手动查找和检查所有jvm启动文件要容易得多。现在jvm配置文件的好坏不是我们的构建过程的一部分或检查到源代码控制。
2 个解决方案
#1
53
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.
这应该显示所有JVM参数。
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.
注意:我们在VCS中也没有JVM参数,但在数据库中,我们自己的生成器中的启动器读取。这样,我们可以远程更改这些值,而无需重新部署JVM参数文件设置。
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 :
您将在本文中找到各种JVM工具(来自“Dustin的软件开发Cogitations and Speculations”),包括Java Application Launcher链接到:
-
ManagementFactory.getRuntimeMXBean(
) call - ManagementFactory.getRuntimeMXBean()调用
-
getInputArguments()
javadoc - 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)
- 从Java访问JVM参数(例如,确定JVM是否在调试模式下运行,以便更改应用程序的“网格初始化”逻辑)
- Annotation Type MXBean
- 注释类型MXBean
- MXBean Java Tutorial
- MXBean Java教程
This technique takes advantage of Platform MXBeans available since J2SE 5 (custom MXBeans support was added in Java SE 6).
此技术利用了自J2SE 5以来可用的Platform MXBeans(Java SE 6中添加了自定义MXBeans支持)。
Two useful sources of information on the JVM parameters available when using Sun's JVM are:
使用Sun的JVM时,有关JVM参数的两个有用信息来源是:
- A Collection of JVM Options and
- JVM选项和集合的集合
- Charles Nutter's Favorite Hotspot JVM Flags.
- Charles Nutter最喜欢的Hotspot JVM标志。
Both of these resources list and describe some/all of the not-recommended-for-the-casual-developer double
X
arguments (-XX
) that are available.这两个资源都列出并描述了一些/所有可用的不推荐用于临时开发人员的双X参数(-XX)。
#2
9
With Java 7 or later it's as easy as
使用Java 7或更高版本,它就像
java -XshowSettings:all
java -XshowSettings:全部
#1
53
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.
这应该显示所有JVM参数。
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.
注意:我们在VCS中也没有JVM参数,但在数据库中,我们自己的生成器中的启动器读取。这样,我们可以远程更改这些值,而无需重新部署JVM参数文件设置。
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 :
您将在本文中找到各种JVM工具(来自“Dustin的软件开发Cogitations and Speculations”),包括Java Application Launcher链接到:
-
ManagementFactory.getRuntimeMXBean(
) call - ManagementFactory.getRuntimeMXBean()调用
-
getInputArguments()
javadoc - 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)
- 从Java访问JVM参数(例如,确定JVM是否在调试模式下运行,以便更改应用程序的“网格初始化”逻辑)
- Annotation Type MXBean
- 注释类型MXBean
- MXBean Java Tutorial
- MXBean Java教程
This technique takes advantage of Platform MXBeans available since J2SE 5 (custom MXBeans support was added in Java SE 6).
此技术利用了自J2SE 5以来可用的Platform MXBeans(Java SE 6中添加了自定义MXBeans支持)。
Two useful sources of information on the JVM parameters available when using Sun's JVM are:
使用Sun的JVM时,有关JVM参数的两个有用信息来源是:
- A Collection of JVM Options and
- JVM选项和集合的集合
- Charles Nutter's Favorite Hotspot JVM Flags.
- Charles Nutter最喜欢的Hotspot JVM标志。
Both of these resources list and describe some/all of the not-recommended-for-the-casual-developer double
X
arguments (-XX
) that are available.这两个资源都列出并描述了一些/所有可用的不推荐用于临时开发人员的双X参数(-XX)。
#2
9
With Java 7 or later it's as easy as
使用Java 7或更高版本,它就像
java -XshowSettings:all
java -XshowSettings:全部