读取 Java JVM 启动参数(例如 -Xmx)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1518213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read Java JVM startup parameters (eg -Xmx)
提问by Bob Albright
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 网络服务器可能配置正确,也可能配置不正确,我想将其添加到启动代码检查中。对我来说,签入一段部署在各处的 Java 代码比手动查找和检查所有 jvm 启动文件要容易得多。现在,无论好坏,jvm 配置文件都不是我们构建过程的一部分,也不是检查到源代码控制中。
采纳答案by VonC
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 Launcherlinks to :
您将在本文(来自“Dustin's Software Development Cogitations and Speculations”)中找到要使用的各种JVM 工具的一个很好的总结,包括 指向以下内容的Java Application Launcher链接:
ManagementFactory.getRuntimeMXBean(
)callgetInputArguments()
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
ManagementFactory.getRuntimeMXBean(
)调用getInputArguments()
文档- 从 Java 访问 JVM 参数(例如,确定 JVM 是否在调试模式下运行,以更改应用程序的“网格初始化”逻辑)
- 注释类型 MXBean
- MXBean Java 教程
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 Optionsand
- 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.
该技术利用了自J2SE 5以来可用的 Platform MXBeans (Java SE 6 中添加了自定义 MXBeans 支持)。
使用 Sun 的 JVM 时,关于 JVM 参数的两个有用信息来源是:
- 一组 JVM 选项和
- Charles Nutter 最喜欢的热点 JVM 标志。
这两个资源都列出并描述了一些/所有不推荐给休闲开发人员的可用双
X
参数 (-XX
)。
回答by Mike Duigou
With Java 7 or later it's as easy as
使用 Java 7 或更高版本,它就像
java -XshowSettings:all
java -XshowSettings:all