Garbage collection Interview Question

  • Fragmentation vs. Garbage Collection Pauses
  • Compaction is performed during garbage collection while all Java threads are paused. Compaction of a large area with many objects will thus increase the garbage collection pause times.
    Insufficient compaction will lead to fragmentation of the heap, which leads to lower performance. If the fragmentation increases over time, the JRockit JVM will eventually be forced to either do a full compaction of the heap, causing a long garbage collection pause, or throw an OutOfMemoryError.
    If your application shows performance degradation over time in a periodic manner, such that the performance degrades until it suddenly pops back to excellent, just to start degrading again, you are most likely experiencing fragmentation problems. The heap becomes more and more fragmented for each old collection until finally object allocation becomes impossible and the JVM is forced to do a full compaction of the heap.
    The full compaction eliminates the fragmentation, but only until the next garbage collection. You can verify this by looking at -Xverbose:memory outputs, monitoring the JVM through the Management Console in JRockit Mission Control or by creating a JRA recording and examining the garbage collection data. If you see that the amount of used heap after each old collection keeps increasing over time until it hits the roof, and then drops down again at the next old collection, you are experiencing a fragmentation problem.