Java 在运行时设置 JVM 堆大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/763295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:11:03  来源:igfitidea点击:

Setting JVM heap size at runtime

javajvmruntimeheap

提问by feiroox

Is there a way to set heap size from a running Java program?

有没有办法从正在运行的 Java 程序中设置堆大小?

采纳答案by Michael Borgwardt

No.

不。

What you can do with an app that has very variable heap requirements is to set your max heap size very high with -Xmxand tune -XX:MaxHeapFreeRatioand -XX:MinHeapFreeRatioso that the app will not hang on to a lot of memory when the heap shrinks (it does that with default settings).

您可以使用具有非常多变的堆要求的应用程序将最大堆大小设置为非常高-Xmx并进行调整-XX:MaxHeapFreeRatio-XX:MinHeapFreeRatio以便在堆收缩时应用程序不会占用大量内存(它使用默认设置来实现) )。

But note that this may cause performance problems when the memory actually used by the app varies both strongly and quickly - in that case you're better off having it hang on to all the memory rather than give it back to the OS only to claim it again a second later. You might also want to fiddle with the GC optionsto ensure that the GC doesn't leave too much unclaimed objects lying around, which it tends to do when there's a lot of room for the heap to grow, and which would defeat the goal of wanting the heap size to adjust to the app's needs.

但请注意,当应用程序实际使用的内存变化很大且变化很快时,这可能会导致性能问题 - 在这种情况下,您最好将它挂在所有内存上,而不是将其返回给操作系统只是为了声明它一秒钟后再次。您可能还想摆弄GC 选项,以确保 GC 不会留下太多无人认领的对象,当堆有很大的增长空间时,它往往会这样做,这会破坏 GC 的目标希望堆大小适应应用程序的需求。

回答by thSoft

According to http://www.dreamincode.net/forums/showtopic96263.htm, you can't do this at runtime, but you can spawn another process with a different heap size.

根据http://www.dreamincode.net/forums/showtopic96263.htm,您不能在运行时执行此操作,但您可以使用不同的堆大小生成另一个进程。

回答by Andrew Hare

You can tweak those settings when you start your application but once the JVM is up and running those values cannot be changed. Something like this:

您可以在启动应用程序时调整这些设置,但是一旦 JVM 启动并运行,这些值就无法更改。像这样的东西:

java -Xms32m -Xmx512m FooBar

java -Xms32m -Xmx512m FooBar

will set the minimum heap size to 32MB and the maximum heap size to 512MB. Once these are set, you cannot change them within the running program.

将最小堆大小设置为 32MB,将最大堆大小设置为 512MB。这些设置完成后,您将无法在正在运行的程序中更改它们。

回答by bruno conde

If I understand your question correctly, you're trying to change the heap size at runtime. I don't see any reason why this should be possible. Set the heap size at startupusing the -XmxJVM option. I also advise you to set the -Xmsoption only if you absolutely need to. This option sets the initial amount of head memory that is allocated for the JVM.

如果我正确理解您的问题,那么您正在尝试在运行时更改堆大小。我没有看到任何理由为什么这应该是可能的。使用JVM 选项在启动时设置堆大小-Xmx。我还建议您-Xms仅在绝对需要时才设置该选项。此选项设置为 JVM 分配的初始内存量。

You should know how your application behaves in terms of memory. Set the the value of -Xmxwisely. If your app is some kind of a server app you can set a higher value, otherwise compromise your choice with other possible apps running in client machines and of course available memory.

您应该知道您的应用程序在内存方面的表现。-Xmx明智地设置值。如果您的应用程序是某种服务器应用程序,您可以设置更高的值,否则会与在客户端计算机上运行的其他可能的应用程序以及可用内存做出妥协。

回答by Jé Queue

The consensus may indeed be that this is not possible, but we should be looking at the JVM source to see how it can be ergonomically controlled. It would be very nice to be able to have a JVMTI agent be able to adjust the heap/perm/tenured/new/&c sizing online/at runtime.

共识可能确实是不可能的,但我们应该查看 JVM 源代码,看看它是如何以符合人体工程学的方式控制的。如果 JVMTI 代理能够在线/在运行时调整堆/永久/终身/新/&c 大小,那将是非常好的。

What would this do? it would allow agents to infer sizing adjustments based upon performance or footprint goals which will be important when moving JVMs into the Cloud.

这会做什么?它将允许代理根据性能或足迹目标推断大小调整,这在将 JVM 移动到云中时非常重要。

回答by Sasha Firsov

I asked same question to myself. And unlike answers above there is something I can do about myapp increasing max heap JVM size. If app is a web server in cluster mode, I could start a new instance with changed min/max heap size and than shutdown initial instance. That shall be especially simple in GlassFish where you have management instance separated from nodeAgent(app server clustered instance) JVM.

我问自己同样的问题。与上面的答案不同,我可以对我的应用程序增加最大堆 JVM 大小做一些事情。如果应用程序是集群模式下的 Web 服务器,我可以启动一个具有更改的最小/最大堆大小的新实例,而不是关闭初始实例。这在 GlassFish 中尤其简单,您的管理实例与 nodeAgent(应用服务器集群实例)JVM 分离。

Since many JVM applications are web apps, I think it worth to preserve in this blog.

由于许多 JVM 应用程序都是 Web 应用程序,因此我认为值得保留在此博客中。

回答by Peter Lawrey

You can use the -mxoption on startup (also known as -Xmx) This is maximum size you should ever need in which cause you shouldn't need to set it to more than the maximum size you will ever need.

您可以在启动时使用-mx选项(也称为-Xmx)这是您应该需要的最大大小,因为您不需要将其设置为超过您永远需要的最大大小。

However, a work around is to have the main() check the maximum size and restart the java if the maximum size is not as desired. i.e. start another java program and die.

但是,解决方法是让 main() 检查最大大小,如果最大大小不符合要求,则重新启动 java。即启动另一个java程序并死。