Force full garbage collection when memory occupation goes beyond a certain threshold

Posted by Silvio Donnini on Stack Overflow See other posts from Stack Overflow or by Silvio Donnini
Published on 2010-03-15T16:59:46Z Indexed on 2010/03/15 17:19 UTC
Read the original article Hit count: 456

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

© Stack Overflow or respective owner

Related posts about java

Related posts about garbage-collection