简体   繁体   English

如何配置JVM仅在确实需要时才分配内存?

[英]How to configure JVM to allocate memory only when is really necessary?

I have an application that can be executed when I use the jvm command -Xmx65m . 我使用jvm命令-Xmx65m时可以执行一个应用程序。 Although it is running fine, I want to allow the application to consume more memory, because it has some features that require it. 尽管它运行良好,但我希望允许该应用程序使用更多的内存,因为它具有某些需要它的功能。 The problem is that if I increase the -Xmx option, the JVM will alocate more memory to run the features that it can handle with only 65 MB. 问题是,如果增加-Xmx选项,JVM将分配更多的内存来运行仅65 MB即可处理的功能。

Is it possible to configure the JVM to only request more memory to the SO only when it is running out of options and it is about to throw an OutOfMemoryError? 是否可以将JVM配置为仅在SO选项即将用尽并且即将抛出OutOfMemoryError时才向SO请求更多内存?

Please add the min , max memory settings. 请添加最小,最大内存设置。 So that JVM can start with the min required memory and keeps allocating more memory as and when its required. 这样,JVM可以从所需的最小内存开始,并在需要时不断分配更多内存。

-Xms65m -Xmx512m -Xms65m -Xmx512m

Hope this helps. 希望这可以帮助。

The JVM reserves the maximum heap size as virtual memory on start up but it only uses the amount it needs (even if you set a minimum size it might not use that much) If it uses a large amount of memory but doesn't need it any more it can give back to the OS (but often doesn't AFAIK) JVM在启动时将最大堆大小保留为虚拟内存,但是它仅使用所需的数量(即使您设置了最小大小,它也可能不会使用那么多的内存),如果它使用了大量的内存但不需要它它可以将其还给操作系统(但通常不是AFAIK)

Perhaps you are not seeing a gradual increase as your maximum is so small. 由于最大值太小,也许您看不到逐渐增加的趋势。 Try it with a maximum of -mx1g and look in jvisualvm as to how the maximum heap size grows. 尝试使用最大-mx1g的值,并在jvisualvm查看最大堆大小如何增长。

Is it possible to configure the JVM to only request more memory to the SO only when it is running out of options and it is about to throw an OutOfMemoryError? 是否可以将JVM配置为仅在SO选项即将用尽并且即将抛出OutOfMemoryError时才向SO请求更多内存?

As the JVM gets close to finally running out of space, it runs the GC more and more frequently, and application throughput (in terms of useful work done per CPU second) falls dramatically. 随着JVM几乎快要用完空间,它越来越频繁地运行GC,并且应用程序吞吐量(就每CPU秒完成的有用工作而言)急剧下降。 You don't want that happening if you can avoid it. 如果可以避免,您不希望发生这种情况。

There is one GC tuning option that you could use to discourage the JVM from growing the heap. 您可以使用一个GC调整选项来阻止JVM增大堆。 The -XX:MinHeapFreeRatio option sets the "minimum percentage of heap free after GC to avoid expansion". -XX:MinHeapFreeRatio选项设置“ GC后为避免扩展而释放堆的最小百分比”。 If you reduce this from the default value of 40% to (say) 20% the GC will be less eager to expand the heap. 如果将其从默认值40%减少到(例如)20%,GC将不那么渴望扩展堆。

The down-side is that if you reduce -XX:MinHeapFreeRatio , the JVM as a whole will spend a larger percentage of its time running the garbage collector. -XX:MinHeapFreeRatio的一面是,如果减少-XX:MinHeapFreeRatio ,则整个JVM将花费其较大比例的时间来运行垃圾收集器。 Go too far and the effect could possibly be quite severe. 走得太远,效果可能会非常严重。 (Personally, I would not recommend changing this setting at all ... ) (就个人而言,我不建议您完全更改此设置...)

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

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