Simeon
Simeon

Reputation: 7790

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,

Upvotes: 12

Views: 6328

Answers (7)

Philipp Wendler
Philipp Wendler

Reputation: 11423

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.

Upvotes: 5

Fredrik LS
Fredrik LS

Reputation: 1480

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/

Upvotes: 7

matt freake
matt freake

Reputation: 5090

Garbage collection is quite a large area, but what you say is correct for full collections (there are other types)

One thing to be aware of is that -Xmx sets the maximum heap size but there is also an -Xms, which is the minumum heap size. Your application may start with only the minimum configured. Then if the memory used reaches that, it will trigger a full garbage collection AND increase the amount of heap available, from the minimum (-Xmx) up to some value less than or equal to the maximum (-Xmx). This can happen several times, until the maximum is reached. After that, it cannot increase the heap anymore but garbage collections will continue to happen when that maximum is reached.

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340953

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.

Upvotes: 11

Michael Borgwardt
Michael Borgwardt

Reputation: 346476

IIRC the guarantee is that a full GC will be performed before an OutOfMemoryError is thrown. Since exceeding the heap size limit must result in such an error, that implies you'll always have at least one full GC run when the limit is reached.

Upvotes: 1

Daniel
Daniel

Reputation: 28104

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

Upvotes: 3

sgowd
sgowd

Reputation: 2262

Yes, if you don't still find memory it will raise OutOfmemory Error. I understand it like this.

Upvotes: 1

Related Questions