有没有办法从正在运行的 Java 程序中设置堆大小?
Is there a way to set heap size from a running Java program?
没有.
对于堆要求非常多变的应用,您可以使用 -Xmx
将最大堆大小设置得非常高,并调整 -XX:MaxHeapFreeRatio
和 -XX:MinHeapFreeRatio
以便应用程序在堆收缩时不会挂起大量内存(它使用默认设置执行此操作).
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).
但请注意,当应用实际使用的内存变化剧烈且迅速时,这可能会导致性能问题 - 在这种情况下,您最好将其挂在所有内存上,而不是仅将其返回给操作系统一秒钟后再次申领.您可能还想摆弄 GC 选项 以确保 GC不会留下太多无人认领的对象,当堆有很大的增长空间时,它往往会这样做,这会破坏希望堆大小适应应用程序需求的目标.
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.
这篇关于在运行时设置 JVM 堆大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!