Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the JVM force garbage collection when it reaches its -Xmx limit?

The question is basically contained in the title.

Say you have an application that has reached its JVM -Xmx limit. When that application requires more memory is garbage collection forced ? (in the HotSpot JVM)

A second odd thing I can't explain is that I currently have an application server which is ran with -Xmx=2048m, the "top" command (on linux) reports 2.7g for its process.

So how/when is an application allowed to exceed its -Xmx ?

Thanks,

like image 393
Simeon Avatar asked Feb 21 '12 09:02

Simeon


4 Answers

Yes, the JVM will certainly call the GC if it reaches the heap limit (and probably much sooner). If this doesn't help, it will throw OutOfMemoryErrors.

The reason why you are seeing a bigger process memory consumption is that the -Xmx option only limits the Java heap space (where the Java objects are allocated on). There are several other memory regions used by the JVM additionally: space for Thread stacks, the "PermGen" (where the classes and their code is stored), "direct" memory allocated via ByteBuffers, memory allocated by native libraries, etc. For some of these additional memory regions there exist other configuration options which allow to limit them, for example -Xss, but some are even out of control of the JVM.

like image 60
Philipp Wendler Avatar answered Nov 10 '22 22:11

Philipp Wendler


Actually the normal GC is triggered when young generation is full (not the whole heap) and major GC is triggered when there is no space left in survivor space so some objects need to be migrated to old generation.

like image 36
Tomasz Nurkiewicz Avatar answered Nov 10 '22 22:11

Tomasz Nurkiewicz


The Xmx parameter does only specify the size of the heap. The Java process takes more memory since the heap is only one part of the Java process, I guess you also have other stuff that the java process contains like native libraries, the perm gen and also native memory allocations made by the application.

Here's an nice article describing memory allocation: http://www.ibm.com/developerworks/java/library/j-nativememory-linux/

like image 35
Fredrik LS Avatar answered Nov 10 '22 22:11

Fredrik LS


This is usually the case, althought GC is normally triggered much sooner, depending on the Garbage Collector you use.

like image 3
Daniel Avatar answered Nov 10 '22 22:11

Daniel