Java Basics  «Prev  Next»

Lesson 9Garbage collection in Java
ObjectiveBe able to describe Java's process of garbage collection.

Java Garbage Collection

Mark and sweep

Is there any significant difference betweent the "Mark and sweep" garbage collection algorithm developed by Sun Microsystems when compared to the current JVM used by Oracle?
The "Mark and Sweep" garbage collection algorithm and the current Java Virtual Machine (JVM) by Oracle are two distinct entities, albeit related in their roles within Java's ecosystem. Specifically, the Mark and Sweep algorithm is one methodology for garbage collection, the process of automatically reclaiming unused memory during program execution. On the other hand, Oracle's JVM is an execution environment that interprets and runs compiled Java bytecodes. It is the JVM that may utilize garbage collection algorithms such as Mark and Sweep, amongst others, in the management of memory. Sun Microsystems, the original developer of Java, implemented the Mark and Sweep algorithm as a foundational technique for garbage collection within the JVM. The algorithm marks live objects during a traversal of the object graph from root references and then sweeps through the heap to deallocate memory that is not marked. This results in a two-phase process: the "mark" phase identifies all live objects, and the "sweep" phase removes the dead ones, freeing up memory space.
Oracle, which acquired Sun Microsystems in 2010, has continued to evolve the JVM and its garbage collection capabilities. Modern JVMs like Oracle's HotSpot include a variety of garbage collection algorithms designed to optimize different aspects of performance. For instance, the Garbage-First (G1) collector aims to provide high throughput and low latency with a more partitioned heap management approach. Other collectors like the Parallel collector or the Concurrent Mark-Sweep (CMS) collector offer alternatives optimized for specific scenarios, such as high throughput or minimal pause times, respectively. These modern algorithms often employ more advanced techniques, such as generational garbage collection, parallelization, and concurrency to improve performance metrics. Consequently, while the Mark and Sweep algorithm may still be employed in some contexts, or as part of more complex algorithms, it can be considered rudimentary compared to the comprehensive set of algorithms available in Oracle's current JVM implementations. In summary, while the original Mark and Sweep algorithm served as a foundational technique for memory management in the JVM developed by Sun Microsystems, Oracle has significantly advanced the state of garbage collection in its contemporary JVM offerings. These advancements have led to a suite of garbage collection algorithms designed to meet a broader range of application requirements, performance characteristics, and operational constraints. Therefore, while Mark and Sweep may remain conceptually significant, its role in modern Oracle JVMs is part of a larger, more sophisticated memory management ecosystem

Sun JVM Mark and Sweep

The garbage collector in Sun's implementation of Java works by using an approach called mark and sweep. The idea is the garbage collector starts at a number of root objects that the Java runtime itself uses to keep track of the objects and variables in your program. Then the garbage collector begins to "walk the links," following references to find which objects these lead to.

Memory Management

If you are coming from the world of C and C++, not freeing the memory used by an object might go against your instincts.
But this is the only way to go in Java. New programmers often ask if there is a way to free an object themselves. The answer is no.
Java is completely in charge of memory. (You can request that Java perform garbage collection in general if you'd like to, but this is different from indicating that a particular object should be garbage collected.
Forcing garbage collection is discussed below.)

Each time the garbage collector finds an object, it marks it.
After it has walked all the links, it sweeps away all the objects that remain unmarked.

finalize

When the garbage collector decides to reclaim memory once occupied by a particular object, it first invokes that object's finalize() method.
This is your notification that the object is about to go away permanently.
You might take this time to perform any cleanup you think is necessary.
For example, here's a simple finalize() method that indicates when the object is going away:

protected void finalize() throws Throwable {
  System.out.println("good-bye");
  super.finalize();
}

We are invoking our superclass' finalize() method just in case our class inherits from a finalize() method that performs some cleanup.
But by default, the finalize() method, as defined in class Object, does nothing. You can suggest garbage collection to occur.