Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What triggers a full garbage collection in Java?

Tags:

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.

like image 587
Paweł Krupiński Avatar asked Mar 03 '12 13:03

Paweł Krupiński


People also ask

What triggers full GC?

A Full GC will be triggered whenever the heap fills up. In such a case the young generation is collected first followed by the old generation.

What causes high garbage collection?

The root cause of high-frequency garbage collection is object churn—many objects being created and disposed of in short order. Nearly all garbage collection strategies are well suited for such a scenario; they do their job well and are fast. However, this still consumes resources.

What is full garbage collection in Java?

Garbage collection in Java is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

How does Java know when to garbage collect?

As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.


1 Answers

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.

like image 136
Paweł Krupiński Avatar answered Sep 23 '22 18:09

Paweł Krupiński