简体   繁体   English

在运行时设置JVM堆大小

[英]Setting JVM heap size at runtime

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

No. 没有。

What you can do with an app that has very variable heap requirements is to set your max heap size very high with -Xmx and tune -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio so 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 options to 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不会留下太多无人认领的物体,当堆有很大的空间来生长时,它往往会这样做,这会破坏目标希望堆大小适应应用程序的需要。

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

You can tweak those settings when you start your application but once the JVM is up and running those values cannot be changed. 您可以在启动应用程序时调整这些设置,但是一旦JVM启动并运行,这些值就无法更改。 Something like this: 像这样的东西:

java -Xms32m -Xmx512m FooBar

will set the minimum heap size to 32MB and the maximum heap size to 512MB. 将最小堆大小设置为32MB,最大堆大小设置为512MB。 Once these are set, you cannot change them within the running program. 设置完成后,您无法在正在运行的程序中更改它们。

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. 可能确实存在共识,这是不可能的,但我们应该关注JVM源,看看它是如何受到人体工程学控制的。 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. 能够让JVMTI代理能够在运行时调整堆/ perm / tenured / new /&c sizing在线/非常好。

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移动到云中时非常重要。

You can use the -mx option 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. 但是,解决方法是让main()检查最大大小,如果最大大小不符合要求,则重新启动java。 ie start another java program and die. 即启动另一个java程序并死掉。

I asked same question to myself. 我问自己同样的问题。 And unlike answers above there is something I can do about my app increasing max heap JVM size. 与上面的答案不同,我可以做一些关于我的应用程序增加最大堆JVM大小的事情。 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. 如果app是集群模式下的Web服务器,我可以启动一个更新的最小/最大堆大小而不是关闭初始实例的新实例。 That shall be especially simple in GlassFish where you have management instance separated from nodeAgent(app server clustered instance) JVM. 这在GlassFish中应该特别简单,其中您将管理实例与nodeAgent(app server clustered instance)JVM分开。

Since many JVM applications are web apps, I think it worth to preserve in this blog. 由于许多JVM应用程序都是Web应用程序,因此我认为值得在此博客中保留。

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 startup using the -Xmx JVM option. 使用-Xmx JVM选项在启动时设置堆大小。 I also advise you to set the -Xms option only if you absolutely need to. 我还建议您仅在绝对需要时设置-Xms选项。 This option sets the initial amount of head memory that is allocated for the JVM. 此选项设置为JVM分配的初始内存量。

You should know how your application behaves in terms of memory. 您应该知道应用程序在内存方面的行为方式。 Set the the value of -Xmx wisely. 明智地设置-Xmx的值。 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. 如果您的应用程序是某种服务器应用程序,您可以设置更高的值,否则会损害您在客户端计算机中运行的其他可能应用程序以及可用内存的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM