Mark and Sweep

First of all, the JVM is more specific about what constitutes reachability of an object. Instead of the vaguely defined green clouds that we saw on earlier chapters, we have a very specific and explicit set of objects that are called the Garbage Collection Roots:

  • Local variables
  • Active threads
  • Static fields
  • JNI references

The method used by JVM to track down all the reachable (live) objects and to make sure the memory claimed by non-reachable objects can be reused is called the Mark and Sweep algorithm. It consists of two steps:

  • Marking is walking through all reachable objects, starting from GC roots and keeping a ledger in native memory about all such objects
  • Sweeping is making sure the memory addresses occupied by non-reachable objects can be reused by the next allocations.

Different GC algorithms within the JVM, such as Parallel Scavenge, Parallel Mark+Copy or CMS, are implementing those phases slightly differently, but at the conceptual level the process remains similar to the two steps described above.

A crucially important thing about this approach is that the cycles are no longer leaked:

Mark and Sweep Garbage Collection

The not-so-good thing is that the application threads need to be stopped for the collection to happen, as you cannot really count references if they keep changing all the time. Such a situation when the application is temporarily stopped so that the JVM can indulge in housekeeping activities is called a Stop The World pause. They may happen for many reasons, but garbage collection is by far the most popular one.

Plumbr can help you!

In this handbook, we will explain how Garbage Collection works in the JVM and how to get the best out of it. We will also bring up quite a number of possible problems that arise from misusing GC. If you are facing one of these problems, you might want to skip ahead and immediately see how Plumbr can automatically help you in resolving it.

Read more