Java Basics  «Prev  Next»

Lesson 8Reclaiming memory via Garbage Collection
ObjectiveBe able to briefly describe how Java reclaims memory.

Reclaiming Memory via Garbage Collection

Java takes over responsibility for managing memory in the runtime environment by providing garbage collection.

Garbage Collection has changed between Java SE 7 and Java SE 21

Yes, the garbage collection (GC) process in Java has undergone several significant changes between Java SE 7 and Java SE 21, both in terms of the available garbage collectors and improvements in performance and behavior.
Here are the key differences:
  1. Garbage Collectors Available:
    • Java SE 7: Java SE 7 had garbage collectors such as:
      • Serial GC: (default for client-side applications).
      • Parallel GC: (default for server-side applications).
      • CMS (Concurrent Mark-Sweep) GC: A low-pause-time garbage collector.
      • G1 GC: (introduced in Java 7 update 4): Designed to replace CMS with better performance and heap region-based collection.
    • Java SE 21:
      • G1 GC: Now a mature and default garbage collector (starting from Java 9).
      • ZGC (Z Garbage Collector): Introduced in Java 11, this is designed for ultra-low-latency applications, with pause times in the millisecond range, regardless of heap size.
      • Shenandoah GC: Introduced in Java 12 and focused on low-pause-time even in large heaps.
      • Epsilon GC: Introduced in Java 11, this is a "no-op" GC used for testing where garbage collection is disabled.
      • Parallel and Serial GC: These remain options in Java 21, but are no longer the default.
  2. Garbage Collection Algorithms:
    • Java SE 7: Focused more on basic stop-the-world collectors like the Serial and Parallel collectors, which resulted in significant application pauses, especially for large heap sizes.
    • Java SE 21: Newer algorithms like ZGC[1] and Shenandoah[2] are designed to minimize pauses by working concurrently with the application. Both collectors use advanced techniques to keep pause times short (often below 10 ms) even with large heaps (in the range of terabytes).
  3. Garbage Collection Tuning:
    • Java SE 7: Tuning options in Java 7 were more limited, and collectors like CMS required more manual tuning to prevent long pause times.
    • Java SE 21: G1 GC has become more efficient, with fewer tuning parameters required to get good performance. ZGC and Shenandoah are designed to require very minimal tuning out of the box for applications that need low-latency garbage collection.
  4. Concurrent and Pause-Time Improvements:
    • Java SE 7: Collectors like CMS had concurrent collection phases, but still introduced pause times during the stop-the-world phases, particularly during the "remark" and "cleanup" steps.
    • Java SE 21: ZGC and Shenandoah GC aim to reduce stop-the-world pauses to a bare minimum, working in the background while the application runs. G1 GC also improved significantly in reducing pause times for most applications.
  5. Large Heap and Low-Latency Requirements:
    • Java SE 7: G1 GC was aimed at reducing pauses for large heap applications, but was still evolving in terms of stability and performance.
    • Java SE 21: ZGC and Shenandoah GC specifically target ultra-large heap sizes (multiple TBs) with a focus on low-latency performance, making Java a strong choice for modern large-scale systems requiring minimal interruptions.


Summary of Changes:
  • More advanced collectors: G1, ZGC, and Shenandoah have become the go-to collectors.
  • Lower pause times: New collectors offer low-latency and concurrent garbage collection.
  • More automation: Less manual tuning is required for most use cases.

In essence, between Java SE 7 and Java SE 21, Java's garbage collection process has become far more efficient, flexible, and scalable, offering developers more options to manage different application requirements.

What is garbage collection?

When you create a new object using new, you are asking the Java runtime to allocate memory to maintain this object. However, nowhere have you stated exactly how much memory Java needs to allocate. And it's never necessary to tell Java to release the memory. Memory management is Java's responsibility.
Garbage collection:
Java's way of reclaiming memory that your program has allocated at some point during its execution but can no longer access.
The ability to reclaim memory that your program can no longer access is known as garbage collection. The mechanism responsible for garbage collection runs continually as a separate thread. This way, Java can continually check for memory that should be reclaimed.
  • GC Example The objects reclaimed are those that you can no longer access from a program.
    For example, take a look at the following snippet:
    String s = new String("test");
    // . . . use s for a while . . .
    s = null;
    

    As soon as the variable s is set to null (which indicates there is no object that s currently references), there is no way for your program to access the String instance you allocated.
    Java recognizes this object is a candidate for garbage collection and will reclaim and use this memory for other objects if it runs out of space.
    Next, let's examine how Java uses mark and sweep to perform garbage collection.


Consider the following code:
class MyClass { }
public class TestClass {
 MyClass getMyClassObject() {
  MyClass mc = new MyClass(); // 1          
  return mc; // 2    
}
 public static void main(String[] args) {
  TestClass tc = new TestClass();// 3       
  MyClass x = tc.getMyClassObject(); // 4       
  System.out.println("got myclass object"); // 5       
  x = new MyClass(); // 6       
  System.out.println("done"); // 7    
 }
}

After what line will the MyClass object created at line 1 be eligible for garbage collection?
Select 1 option
  1. 2
  2. 5
  3. 6
  4. 7
  5. Never till the program ends.


Answer: c
Explanation: c) At line 6, x starts pointing to a new MyClassObject and no reference to the original MyClass object is left. Garbage Collection:
Although not mentioned explicitly in the objectives, the exam 1Z0-803 has a few basic questions on garbage collection.
All you need to know is:
  1. An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
  2. You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System.gc();

[1] ZGC :ZGC is a concurrent garbage collector that performs most of its work while Java threads continue running, minimizing pauses. It uses colored pointers and relocation techniques to efficiently identify and reclaim unused memory, without the need for traditional compaction.
[2] Shenandoah: Shenandoah GC prioritizes minimizing pause times by performing most garbage collection tasks concurrently with the running Java application. This involves concurrently marking live objects, compacting the heap to reduce fragmentation, and cleaning up unreachable objects, resulting in a smoother user experience even with large heap sizes.

SEMrush Software