java - 如何选择jvm堆大小?

标签 java jvm heap-memory

我通常对 jvm 堆大小做的是将最大值设置得非常高以避免臭名昭著的 OutOfMemoryException。

但是,这种策略(或缺乏策略)似乎并不是很聪明。 :-)。

我的问题是如何选择最小值和最大值,以及两者之间的差异(max-min 应该小还是大?)。 例如,来自 here :

if the initial heap is too small, the Java application startup becomes slow as the JVM is forced to perform garbage collection frequently until the heap has grown to a more reasonable size. For optimal startup performance you should set the initial heap size to the same as the maximum heap size.

谢谢。

最佳答案

My question is how to choose the min and max values, and the difference between the two (should max-min be small or big?)

简答:不要猜测,分析您的应用程序。

jconsole can give you useful high-level data例如对主要驻留集与我们通常分配和垃圾收集的 transient 数据的感觉。如果您查看该显示器的内存选项卡,您会看到通常类似于锯齿的东西。锯齿的下角大约是我通常设置堆最小值的位置,而我会使用锯齿的峰值或斜率来试验堆最大值。如果您的 dentry 非常陡峭,您可能会考虑使用大堆来延迟垃圾收集。但是,如果不是,您可以尝试较小的堆最大值,看看是否会为您机器上的其他进程留下更多资源(例如)。

您还应该考虑 server VM因为这会导致不同的垃圾收集行为。

话虽如此,您还应该使用更详细的工具,例如 jvisualvm to profile the memory usage of your process .您可能存在可以调整或消除的内存泄漏或贪婪分配器。这将彻底改变您的堆需求。

关于java - 如何选择jvm堆大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651225/

相关文章:

java - Java 字节码有通用解析器吗?

c++ - 在 Visual C++ 中删除调用后检测到堆损坏?

encoding - JRE国际化了吗?

java - 使用java字符串的两个日期语句之间的SQL

java - jEdit+JythonInterpreter : how to import java class?

rust - 当所有权从盒子中转移出来时,内存中会发生什么?

c++ - 在额外函数中使用 delete/new 时堆损坏

java - Android OpenGL ES 正交奇怪的长宽比

java - 为什么 "Inside the Java Virtual Machine"说 "NewbornBaby need not be loaded"?

java - 为什么大多数java.lang.reflect.Array类方法都是 'native'