I'm currently reading about the JVM and how garbage collection works found here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

It discusses both the Mark and Sweep and the Generational garbage collection concepts. After reading through this tutorial, I'm confused on which garbage collection process the JVM actually uses. The whole time, I thought the JVM used the Mark and Sweep process. Has Mark and Sweep been replaced by Generational garbage collection in newer versions of Java or is the Generational garbage collection just a theory for now and not fully implemented?

Thanks in advance.

Edited by Compton11: Grammar

2 Years
Discussion Span
Last Post by mike_2000_17

If you look at the practical example section, where they show you how to visualize the garbace collector using that VisualGC tool, you can clearly see that the generational GC is what is being used in practice.

The point really is that the "mark-and-sweep" process is the fundamental mechanism by which garbage collection is done. And the mark and sweep is still the way that the generational GC works, it's just that the generational approach is an improvement over a basic mark-and-sweep by creating a distinction between short-lived and long-lasting objects. It is merely taking advantage of the fact that when you are creating and throwing away large quantities of small objects quickly, then it is better to do mark-and-sweep passes much more frequently (to avoid letting the memory usage grow too much or be too full of garbage before collecting it). And conversely, when you have long-lasting objects (e.g., singletons, "main window" object), you can probably just let them sit for a longer time without checking for garbage. So, all that the generational approach does is to split the memory into a section of "young" memory that is marked-and-swept very regularly, and a section of "old" memory that is marked-and-swept far less frequently, with some additional logic to be able to promote things from young to old.

Probably some older JVM versions used a vanilla mark-and-sweep with no generational segmentation. But I think that modern versions are more refined than that. But again, generational GC is not an alternative to mark-and-sweep, it's a refinement of mark-and-sweep that ends up working better in practice. It's kind of like the way intro-sort is a refinement of quick-sort that works better in practice.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.