Is there a way to modify the jvm args from inside of the jvm? Specifically, I want to be able to change the maximum heap size of the jvm from inside of it. Is this possible?
有没有办法从jvm内部修改jvm args?具体来说,我希望能够从内部更改jvm的最大堆大小。这可能吗?
Edit: I guess I should add the reason I wanted to do this. I have a few Java programs that are run on different machines/platforms. These programs have configurations that are sourced at runtime and are different depending on the machine/environment the program's running one. Some of these configurations can be changed at runtime and the various programs automatically update themselves as the configurations change.
编辑:我想我应该添加我想要这样做的原因。我有一些在不同的机器/平台上运行的Java程序。这些程序具有在运行时获取的配置,并且根据程序运行的计算机/环境而不同。其中一些配置可以在运行时更改,并且各种程序会随着配置的更改自动更新。
I wanted heap size to be one of these configuration parameters that is sourced at runtime like the rest of the configuration. If so, then the program could startup (with some default jvm args) then adjust itself based on the retrieved config.
我希望堆大小是这些配置参数之一,它在运行时来源,就像配置的其余部分一样。如果是这样,那么程序可以启动(使用一些默认的jvm args)然后根据检索到的配置调整自己。
7 个解决方案
#1
This is a halfway-serious, completely-off-the-wall hack-thought:
这是一个半途而废的,彻头彻尾的黑客思想:
...what if you spawned a new instance of java (with the new settings) from the current jvm then killed the old process from the new? I have no idea if this will help or not (or even work)...
...如果你从当前的jvm中生成一个新的java实例(使用新设置)然后从新的进程中杀死旧进程怎么办?我不知道这是否有帮助(或甚至工作)......
#2
You cant change those options simply because it compromises the security of the system.
您不能仅仅因为它会危及系统的安全性而改变这些选项。
If an admin wishes to only allow a certain program to have certain capaibilities by setting a security manager, it would be a serious problem if you could turn that off.
如果管理员希望通过设置安全管理员只允许某个程序具有某些功能,那么如果您可以将其关闭,那将是一个严重的问题。
Anyway a program should not be changing stuff like its memory requirements at runtime - these should be known and setup by the administrator. There is no reason why your program should need to do this at runtime. If it really needs to change this perhaps the reason qustion is why doesnt the admin type dude not do it ?
无论如何,一个程序不应该在运行时改变它的内存需求 - 这些应该由管理员知道和设置。您的程序没有理由需要在运行时执行此操作。如果它真的需要改变这个或许qustion的原因是为什么管理员类型的家伙不这样做?
#3
Specifically, I want to be able to change the maximum heap size of the jvm from inside of it. Is this possible?
具体来说,我希望能够从内部更改jvm的最大堆大小。这可能吗?
No.
#4
#5
(Years later), I found a library that accomplishes what I needed at the time I posted. Akuma is a library for daemonizing Java processes. It allows restarting the JVM with new arguments.
(多年以后),我找到了一个库,可以在我发布的时候完成我所需要的。 Akuma是一个用于守护Java进程的库。它允许使用新参数重新启动JVM。
The difference between using this and launching a new process with a bootstrap is that stdin/stdout and other file descriptors are automatically shared.
使用它和使用引导启动新进程之间的区别在于stdin / stdout和其他文件描述符是自动共享的。
#6
As noted by others, this is generally only possible by having a bootstrap program (Java or other language) that invokes the main JVM with the right parameters.
正如其他人所指出的,这通常只能通过使用引导程序(Java或其他语言)来调用具有正确参数的主JVM。
That said, are you sure this is necessary? You mention "maximum heap size". If you are referring to the -Xmx parameter: This is just a limit the VM may never exceed. It does not mean the VM will really use that much, it will do with less if it can.
那就是说,你确定这是必要的吗?你提到“最大堆大小”。如果您指的是-Xmx参数:这只是VM可能永远不会超过的限制。这并不意味着虚拟机会真正使用那么多,如果可以的话,它会用更少的东西。
So you can always set -Xmx to the maximum the system can handle, and let the JVM decide how much it really needs. Why do you think you need to set it dynamically?
因此,您始终可以将-Xmx设置为系统可以处理的最大值,并让JVM确定它真正需要多少。为什么你认为你需要动态设置它?
#7
Dynamically setting -Xmx for instance would be very helpful for controlling projected system resources (primarily footprint) for long running runtimes. One of many ramifications of course is bigger/longer GC overhead.
例如,动态设置-Xmx对于长时间运行的运行时控制预计的系统资源(主要是足迹)非常有用。当然,许多分支之一是更大/更长的GC开销。
I know several large server machines running dozens and dozens of 1G+ heap JVMs. If these runtimes could have their high water mark scaled back during low-use times then you could hard manage your system resources much better.
我知道有几台大型服务器机器运行着几十个和几十个1G +堆JVM。如果这些运行时可以在低使用时间内缩小其高水位标记,那么您可以更好地管理系统资源。
#1
This is a halfway-serious, completely-off-the-wall hack-thought:
这是一个半途而废的,彻头彻尾的黑客思想:
...what if you spawned a new instance of java (with the new settings) from the current jvm then killed the old process from the new? I have no idea if this will help or not (or even work)...
...如果你从当前的jvm中生成一个新的java实例(使用新设置)然后从新的进程中杀死旧进程怎么办?我不知道这是否有帮助(或甚至工作)......
#2
You cant change those options simply because it compromises the security of the system.
您不能仅仅因为它会危及系统的安全性而改变这些选项。
If an admin wishes to only allow a certain program to have certain capaibilities by setting a security manager, it would be a serious problem if you could turn that off.
如果管理员希望通过设置安全管理员只允许某个程序具有某些功能,那么如果您可以将其关闭,那将是一个严重的问题。
Anyway a program should not be changing stuff like its memory requirements at runtime - these should be known and setup by the administrator. There is no reason why your program should need to do this at runtime. If it really needs to change this perhaps the reason qustion is why doesnt the admin type dude not do it ?
无论如何,一个程序不应该在运行时改变它的内存需求 - 这些应该由管理员知道和设置。您的程序没有理由需要在运行时执行此操作。如果它真的需要改变这个或许qustion的原因是为什么管理员类型的家伙不这样做?
#3
Specifically, I want to be able to change the maximum heap size of the jvm from inside of it. Is this possible?
具体来说,我希望能够从内部更改jvm的最大堆大小。这可能吗?
No.
#4
With JRockit you can at least suggest a heap size.
使用JRockit,您至少可以建议堆大小。
JVMFactory.getJVM().getMemorySystem().suggestHeapSize(100*1000*1000);
See JMAPI for more information
有关更多信息,请参阅JMAPI
#5
(Years later), I found a library that accomplishes what I needed at the time I posted. Akuma is a library for daemonizing Java processes. It allows restarting the JVM with new arguments.
(多年以后),我找到了一个库,可以在我发布的时候完成我所需要的。 Akuma是一个用于守护Java进程的库。它允许使用新参数重新启动JVM。
The difference between using this and launching a new process with a bootstrap is that stdin/stdout and other file descriptors are automatically shared.
使用它和使用引导启动新进程之间的区别在于stdin / stdout和其他文件描述符是自动共享的。
#6
As noted by others, this is generally only possible by having a bootstrap program (Java or other language) that invokes the main JVM with the right parameters.
正如其他人所指出的,这通常只能通过使用引导程序(Java或其他语言)来调用具有正确参数的主JVM。
That said, are you sure this is necessary? You mention "maximum heap size". If you are referring to the -Xmx parameter: This is just a limit the VM may never exceed. It does not mean the VM will really use that much, it will do with less if it can.
那就是说,你确定这是必要的吗?你提到“最大堆大小”。如果您指的是-Xmx参数:这只是VM可能永远不会超过的限制。这并不意味着虚拟机会真正使用那么多,如果可以的话,它会用更少的东西。
So you can always set -Xmx to the maximum the system can handle, and let the JVM decide how much it really needs. Why do you think you need to set it dynamically?
因此,您始终可以将-Xmx设置为系统可以处理的最大值,并让JVM确定它真正需要多少。为什么你认为你需要动态设置它?
#7
Dynamically setting -Xmx for instance would be very helpful for controlling projected system resources (primarily footprint) for long running runtimes. One of many ramifications of course is bigger/longer GC overhead.
例如,动态设置-Xmx对于长时间运行的运行时控制预计的系统资源(主要是足迹)非常有用。当然,许多分支之一是更大/更长的GC开销。
I know several large server machines running dozens and dozens of 1G+ heap JVMs. If these runtimes could have their high water mark scaled back during low-use times then you could hard manage your system resources much better.
我知道有几台大型服务器机器运行着几十个和几十个1G +堆JVM。如果这些运行时可以在低使用时间内缩小其高水位标记,那么您可以更好地管理系统资源。