强制JVM尽早收集垃圾并尽早减少使用的系统内存 [英] Force the JVM to collect garbage early and reduce system memory used early

查看:211
本文介绍了强制JVM尽早收集垃圾并尽早减少使用的系统内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们运行一个我们没有开发的Java应用程序。
此应用程序对于某些任务使用了相当多的内存,具体取决于所处理的数据,最高可达4GB。在其他时候,只需要很少的内存,大约300MB。



一旦JVM抓住大量内存,花费很长时间才会收集垃圾甚至更长的时间,直到内存返回到操作系统。这对我们来说是一个问题。



会发生什么如下:JVM需要大量内存才能完成任务,并抓取4GB Ram来创建4GB Heap。然后,处理完成后,内存只能填充30%-50%。内存消耗需要很长时间才能改变。当我触发GC(通过jConsole)时,堆缩小到500MB以下。另一个触发的GC和堆缩小到200MB。有时内存会返回系统,通常不会。



以下是VisualVM的典型截图。堆被收集(使用的堆下降)但堆大小保持不变。只有当我通过执行GC按钮触发GC时,Heap大小才会减小。





我们如何调整GC以更早收集内存?性能和GC-Pause-Times对我们来说不是什么大问题。我们宁愿让更多更早的GCs及时减少内存。
我们如何调整JVM以将内存释放回操作系统,使JVM使用的内存更小?



我确实知道-XX:MinHeapFreeRatio和-XX:MaxHeapFreeRatio,这有点帮助,但用VisualVM看堆显示我们,它并不总是服从。我们将MaxHeapFreeRatio设置为40%,并在VisualVM中看到,堆只填充到大约10%。



我们无法减少最大内存(-Xmx),因为有时,很短的时间内,需要大量的内存。



我们不限于特定的GC。因此,任何可以解决问题的GC都可以应用。



我们使用Oracle Hotspot JVM 1.8

解决方案

我假设你使用HotSpot JVM。

然后你可以使用JVM-Option -XX :InitiatingHeapOccupancyPercent = n (0 <= n <= 100)来强制JVM更频繁地执行垃圾收集。当你将n设置为0时,应该发生常量GC。但我不确定这是否是您的应用程序响应时间的好主意。


We operate a Java application that we did not develop. This application uses quite a lot of memory for certain tasks, depending on the data, that is manipulated, up to 4GB. At other times, very little memory is needed, around 300MB.

Once the JVM grabs hold of a lot of memory, it takes a very long time until the garbage is collected and even longer until memory is returned back to the operating system. That's a problem for us.

What happens is as follows: The JVM needs a lot of memory for a task and grabs 4GB of Ram to create a 4GB Heap. Then, after processing finished, the memory is filled only 30%-50%. It takes a long time for memory consumption to change. When I trigger a GC (via jConsole) the heap shrinks below 500MB. Another triggered GC and the heap shrinks to 200MB. Sometimes memory is returned to the system, often not.

Here is typical screenshot of VisualVM. The Heap is collected (Used heap goes down) but Heap size stays up. Only when I trigger the GC through the "Perform GC"-Button, Heap size is reduced.

How can we tune the GC to collect memory much earlier? Performance and GC-Pause-Times are not much of an issue for us. We would rather have more and earlier GCs to reduce memory in time. And how can we tweak the JVM to release memory back to the operating system, making the memory used by the JVM smaller?

I do know -XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio, which helps a bit, but watching the heap with VisualVM shows us, that it's not always obeyed. We set MaxHeapFreeRatio to 40% and see in VisualVM, that the heap is only filled to about 10%.

We can't reduce the maximum memory (-Xmx) since sometimes, for a short period, a lot of memory is acutally needed.

We are not limited to a particular GC. So any GC that solves the problem best can be applied.

We use the Oracle Hotspot JVM 1.8

解决方案

I'm assuming you use the HotSpot JVM.

Then you can use the JVM-Option -XX:InitiatingHeapOccupancyPercent=n (0<=n<=100) to force the JVM to do garbage collection more often. when you set n to 0, constant GC should take place. But I'm not sure whether this is a good idea regarding your applications response time.

这篇关于强制JVM尽早收集垃圾并尽早减少使用的系统内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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