读取 Java JVM 启动参数(例如 -Xmx) [英] Read Java JVM startup parameters (eg -Xmx)

查看:27
本文介绍了读取 Java JVM 启动参数(例如 -Xmx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚是否有办法从正在运行的 Java 进程中确定 JVM 启动属性.具体来说,我试图找出诸如 -Xmx(最大堆大小)和 -XX:MaxPermSize 之类的参数的存储位置.我正在运行 Sun 的 1.6 jvm.

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.

如果您想知道我为什么要这样做,我有许多 JVM 网络服务器,它们可能配置正确,也可能配置不正确,我想将其添加到启动代码检查中.对我来说,签入一段部署在各处的 Java 代码比手动查找和检查所有 jvm 启动文件要容易得多.现在,无论好坏,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.

推荐答案

尝试:

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 ) );
  }
}

那应该会显示所有 JVM 参数.

That should show all JVM parameters.

注意:我们在 VCS 中也没有 JVM 参数,而是在数据库中,由我们自己的启动器在生产中读取.这样,我们就可以远程更改这些值,而无需重新部署 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.

您会发现各种本文中使用的 JVM 工具(来自 Dustin 的软件开发思考和推测">),包括Java 应用程序启动器链接到:

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

此技术利用了自 J2SE 5 以来可用的 Platform MXBeans(自定义 MXBeans 支持已在 Java SE 6 中添加).

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

有关使用 Sun JVM 时可用的 JVM 参数的两个有用信息来源是:

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

这两个资源都列出并描述了一些/所有不推荐给休闲开发人员使用的双 X 参数 (-XX).

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

这篇关于读取 Java JVM 启动参数(例如 -Xmx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆