JVM 最大堆大小可以是动态的吗? [英] Can the JVM max heap size be dynamic?

查看:20
本文介绍了JVM 最大堆大小可以是动态的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JVM -Xmx 参数允许将 JVM 的最大堆大小设置为某个值.但是,有没有办法使该值动态化?换句话说,我想告诉 JVM看,如果你需要它,就继续从系统中获取 RAM,直到系统退出."

The JVM -Xmx argument lets one set the max heap size for the JVM to some value. But, is there a way to make that value dynamic? In other words, I want to tell the JVM "look, if you need it, just keep taking RAM from the system until the system is out."

询问的两个部分原因:首先,有问题的应用程序可以使用非常广泛的内存,具体取决于用户正在做什么,因此概念上的最小值和最大值相差很远.其次,JVM 似乎在启动时从虚拟内存中保留了最大堆空间.这个特定的应用程序在各种各样的硬件上运行,所以选择一个一刀切"的最大堆空间是很困难的,因为它必须足够低才能在低端硬件上运行,但我们真的喜欢能够利用真正强大的机器(如果有的话).

Two-part reason for asking: First, the app in question can use a really wide range of ram depending on what the user is doing, so the conceptual min and max values are pretty far apart. Second, it would seem that the JVM reserves the max heap space from virtual memory at boot time. This particular app is run on a pretty wide variety of hardware, so picking a "one-size-fits-all" max heap space is hard since it has to be low enough to run on low-end hardware, but we'd really like to be able to take advantage of really beefy machines if they're available.

推荐答案

但是,有没有办法使该值动态化?

But, is there a way to make that value dynamic?

从字面上看,没有.最大堆大小在 JVM 启动时设置,不能增加.

Literally, no. The max heap size is set at JVM launch time and cannot be increased.

在实践中,您可以将最大堆大小设置为平台允许的大小,然后让 JVM 根据需要增加堆大小.这样做有明显的风险;即您的应用程序使用所有内存并导致用户的机器停止运行.但这种风险隐含在您的问题中.

In practice, you could just set the max heap size to as large as your platform will allow, and let the JVM grow the heap as it needs. There is an obvious risk in doing this; i.e. that your application will use all of the memory and cause the user's machine to grind to a halt. But that risk is implicit in your question.

编辑

值得注意的是,有各种 -XX... GC 调整选项,允许您调整 JVM 扩展堆的方式(达到最大值).

It is worth noting that there are various -XX... GC tuning options that allow you to tweak the way that the JVM expands the heap (up to the maximum).

另一种可能性是将您的应用程序分成两部分.应用程序的第一部分完成了确定问题大小"所需的所有准备工作.然后它计算出合适的最大堆大小,并在新的 JVM 中启动应用程序的内存饥渴的第二部分.

Another possibility is to split your application into 2 parts. The first part of the application does all of the preparation necessary to determine the "size" of the problem. Then it works out an appropriate max heap size, and launches the memory hungry second part of the application in a new JVM.

  • 这仅在应用程序可以按上述方式进行合理分区时才有效.

  • This only works if the application can sensibly be partitioned as above.

这仅在可以计算问题大小时才有效.在某些情况下,计算问题大小就等于计算结果.

This only works if it is possible to compute the problem size. In some cases, computing the problem size is tantamount to computing the result.

与让堆增长到最大大小相比,您是否会获得更好的整体性能尚不清楚.

It is not clear that you will get better overall performance than if you just let the heap grow up to a maximum size.

这篇关于JVM 最大堆大小可以是动态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆