当内存占用超过某个阈值时强制进行完整的垃圾收集 [英] Force full garbage collection when memory occupation goes beyond a certain threshold

查看:500
本文介绍了当内存占用超过某个阈值时强制进行完整的垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器应用程序,在极少数情况下,可以分配大块内存。



这不是内存泄漏,因为这些块可以垃圾回收器通过执行完整的垃圾回收来声明正常的垃圾回收可以释放太小的内存量:在这种情况下它是不够的。



当垃圾回收器执行这些完整的GC时它认为合适,即当应用程序的内存占用量接近用-Xmx指定的分配最大值时。如果不是因为这些有问题的内存分配突然爆发,并且可能会导致OutOfMemoryErrors,则可以这样做:

jvm无法快速执行GC以释放所需内存的事实。如果我事先手动调用System.gc(),我可以防止这种情况。无论如何,我不希望自己监视我的jvm的内存分配(或将内存管理插入到我的应用程序的逻辑中)。如果有一种方法可以运行带有内存阈值的虚拟机,那么将会自动执行完整的GC,以便尽早释放我将需要的内存。



长话短说:我需要一种方式(命令行选项?)来配置jvm,以便在内存占用达到某个特定时间时提早释放大量内存(即执行完整的GC)阈值,我不在乎这是否会让我的应用程序每隔一段时间就会变慢。



我到目前为止发现的所有方法都是修改代码的大小,但那不是我所需要的(至少不是直接)。



我会很感激你的建议,

Silvio



PS我正在努力避免大量分配,但可能需要很长时间,同时我的应用程序需要一点稳定性。



更新 :用jvisualvm分析应用程序,我可以看到问题出在老一代

解决方案

From here (这是一个1.4.2页面,但所有Sun应该都有相同的选项JVM):假设你使用CMS垃圾收集器(我相信服务器默认开启),你想要的选项是


$ b

  -XX:CMSInitiatingOccupancyFraction =< percent> 

其中%是正在使用的将触发完整GC的内存的百分比。



在这里插入标准免责声明,与GC参数相混淆可能会给您带来严重的性能问题,并可能因机器等原因而不同。


I have a server application that, in rare occasions, can allocate large chunks of memory.

It's not a memory leak, as these chunks can be claimed back by the garbage collector by executing a full garbage collection. Normal garbage collection frees amounts of memory that are too small: it is not adequate in this context.

The garbage collector executes these full GCs when it deems appropriate, namely when the memory footprint of the application nears the allotted maximum specified with -Xmx.

That would be ok, if it wasn't for the fact that these problematic memory allocations come in bursts, and can cause OutOfMemoryErrors due to the fact that the jvm is not able to perform a GC quickly enough to free the required memory. If I manually call System.gc() beforehand, I can prevent this situation.

Anyway, I'd prefer not having to monitor my jvm's memory allocation myself (or insert memory management into my application's logic); it would be nice if there was a way to run the virtual machine with a memory threshold, over which full GCs would be executed automatically, in order to release very early the memory I'm going to need.

Long story short: I need a way (a command line option?) to configure the jvm in order to release early a good amount of memory (i.e. perform a full GC) when memory occupation reaches a certain threshold, I don't care if this slows my application down every once in a while.

All I've found till now are ways to modify the size of the generations, but that's not what I need (at least not directly).

I'd appreciate your suggestions,

Silvio

P.S. I'm working on a way to avoid large allocations, but it could require a long time and meanwhile my app needs a little stability

UPDATE: analyzing the app with jvisualvm, I can see that the problem is in the old generation

解决方案

From here (this is a 1.4.2 page, but the same option should exist in all Sun JVMs):

assuming you're using the CMS garbage collector (which I believe the server turns on by default), the option you want is

-XX:CMSInitiatingOccupancyFraction=<percent>

where % is the % of memory in use that will trigger a full GC.

Insert standard disclaimers here that messing with GC parameters can give you severe performance problems, varies wildly by machine, etc.

这篇关于当内存占用超过某个阈值时强制进行完整的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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