23
votes

I'm wondering what are the exact situations that trigger Full Garbage Collection in Java.

The obvious ones are:

  • Running out of old gen
  • Running out of perm gen
  • Calling System.gc()

What about other cases that cause full gc? Particularly:

  • Not having enough free space in Survivor Space to copy objects from Eden.
  • Minor collections not being able to cope with allocation rate of new objects (don't know how though).

I'm running Sun Java 1.6 and using Concurrent Mark-Sweep and ParNew for new gen.

2
As Chin Boon suggests, none of those "obvious" ones will necessarily lead to a full GC, it depends entirely on how the active garbage collector algorithm works. In particular, it's my understanding that a lot of garbage collectors more or less ignore System.gc(). (And don't forget that it's even possible for a GC algorithm to not even have a concept of a "full collection" - this might sound far-fetched but I've not seen the G1 collector do so on our apps.) - Andrzej Doyle
What do you think JVM with G1 does when it runs out of heap space? Keep on allocating? It has to stop everything until it can free memory. There is a switch that explicitly turns off System.gc() hint, but I haven't seen CMS or G1 ignoring it by default. - Paweł Krupiński
I guess it depends how you define "full collection" - I don't believe it's a first-class term within the context of the JLS or VM spec. The meaning will vary depending on the particular GC algorithm you're running, and might not even make sense for a given GC impl. What I was trying to point out is that the only possible general answer is "it depends", with all other specifics deferred to the internals of the particular garbage collector. - Andrzej Doyle
I think there is a common denominator to full collection. Full collection is defined as a collection during the whole duration of which the threads are stopped. Even though CMS has a short pause, the whole JVM is suspended for the whole duration of the collection when a full collection happens. Full collection with CMS is actually two CMS runs - one for old gen and one for perm gen. I imagine it's exactly the same with G1. - Paweł Krupiński
This article gives an example of Full collection in G1 (when incremental collection fails): blog.ragozin.info/2011/12/…. If Hotspot GC logs recognize the term Full GC, I think it's quite official. - Paweł Krupiński

2 Answers

4
votes

I've observed one more situation that triggers a full GC in Java Hotspot VM 1.6 64bit on Ubuntu, using Concurrent Mark-Sweep:

If -XX:PermSize value does not equal to -XX:MaxPermSize (e.g. is smaller), an occasional Full GC happens when java needs to expand the PermGen (even though it does not need to allocate more memory than MaxPermSize). So setting -XX:PermSize and -XX:MaxPermSize to be the same seems like a good idea.

2
votes

This greatly depends on your jvm options and the jvm that you at using.

For this reason, I recommend that you look at the book "Java Performance" bý John and Hunt.