Chapter 19 - Garbage Collector Flashcards

1
Q

What is Garbage Collection in Java?

A

Garbage Collection is the process of reclaiming the runtime unused memory by destroying the unused objects.

Java Garbage Collection is the process by which Java programs perform automatic memory management. Java programs compile into bytecode that can be run on a Java Virtual Machine (JVM).

When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

Over the lifetime of a Java application, new objects are created and released. Eventually, some objects are no longer needed. You can say that at any point in time, the heap memory consists of two types of objects:

Live - these objects are being used and referenced from somewhere else
Dead - these objects are no longer used or referenced from anywhere
The garbage collector finds these unused objects and deletes them to free up memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to Dereference an Object in Java

A

The main objective of Garbage Collection is to free heap memory by destroying the objects that don’t contain a reference. When there are no references to an object, it is assumed to be dead and no longer needed. So the memory occupied by the object can be reclaimed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Garbage Collection Roots in Java?

A

Garbage collectors work on the concept of Garbage Collection Roots (GC Roots) to identify live and dead objects.

Examples of such Garbage Collection roots are:

Classes loaded by system class loader (not custom class loaders)
Live threads
Local variables and parameters of the currently executing methods
Local variables and parameters of JNI methods
Global JNI reference
Objects used as a monitor for synchronization
Objects held from garbage collection by JVM for its purposes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Phases of Garbage Collection in Java

A

Mark objects as alive
In this step, the GC identifies all the live objects in memory by traversing the object graph.

When GC visits an object, it marks it as accessible and thus alive. Every object the garbage collector visits is marked as alive. All the objects which are not reachable from GC Roots are garbage and considered as candidates for garbage collection.

Sweep dead objects
After marking phase, we have the memory space which is occupied by live (visited) and dead (unvisited) objects. The sweep phase releases the memory fragments which contain these dead objects.

Compact remaining objects in memory
The dead objects that were removed during the sweep phase may not necessarily be next to each other. Thus, you can end up having fragmented memory space.

Memory can be compacted after the garbage collector deletes the dead objects, so that the remaining objects are in a contiguous block at the start of the heap.

The compaction process makes it easier to allocate memory to new objects sequentially.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Generational Garbage Collection in Java?

A

Java Garbage Collectors implement a generational garbage collection strategy that categorizes objects by age.

In fact most objects have a very short life as shown by the higher values on the left side of the graph. This is why Java categorizes objects into generations and performs garbage collection accordingly.

The heap memory area in the JVM is divided into three sections:
Young generation, old generation, permanent generation.

Young Generation
Newly created objects start in the Young Generation. The Young Generation is further subdivided into:

Eden space - all new objects start here, and initial memory is allocated to them
Survivor spaces (FromSpace and ToSpace) - objects are moved here from Eden after surviving one garbage collection cycle.
When objects are garbage collected from the Young Generation, it is a minor garbage collection event.

When Eden space is filled with objects, a Minor GC is performed. All the dead objects are deleted, and all the live objects are moved to one of the survivor spaces. Minor GC also checks the objects in a survivor space, and moves them to the other survivor space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Old Generation ?

A

Old Generation
Objects that are long-lived are eventually moved from the Young Generation to the Old Generation. This is also known as Tenured Generation, and contains objects that have remained in the survivor spaces for a long time.

There is a threshold defined for the tenure of an object which decides how many garbage collection cycles it can survive before it is moved to the Old Generation.

When objects are garbage collected from the Old Generation, it is a major garbage collection event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Permanent Generation?

A

Permanent Generation
Metadata such as classes and methods are stored in the Permanent Generation. It is populated by the JVM at runtime based on classes in use by the application. Classes that are no longer in use may be garbage collected from the Permanent Generation.

You can use the -XX:PermGen and -XX:MaxPermGen flags to set the initial and maximum size of the Permanent Generation.

MetaSpace
Starting with Java 8, the MetaSpace memory space replaces the PermGen space. The implementation differs from the PermGen and this space of the heap is now automatically resized.

This avoids the problem of applications running out of memory due to the limited size of the PermGen space of the heap. The Metaspace memory can be garbage collected and the classes that are no longer used can be automatically cleaned when the Metaspace reaches its maximum size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Types of Garbage Collectors in Java?

A

Term: Serial GC
Definition: Simplest implementation of GC in Java Virtual Machine, designed for single-threaded environments. Conducts garbage collection events serially in one thread, leading to “stop the world” events causing application freeze.

Term: Parallel GC
Definition: Default implementation of GC in JVM, suitable for medium to large-sized data sets on multiprocessor or multithreaded hardware. Uses multiple threads for minor garbage collection and a single thread for major garbage collection.

Term: Parallel Old GC
Definition: Default version of Parallel GC since Java 7u4, using multiple threads for both Young Generation and Old Generation garbage collection.

Term: CMS (Concurrent Mark Sweep) GC
Definition: Concurrent low pause collector in JVM. Utilizes multiple threads for minor garbage collection similar to Parallel, with major garbage collection running concurrently alongside application processes to minimize “stop the world” events.

Term: G1 (Garbage First) GC
Definition: Designed for multi-threaded applications with large heap sizes, replacing CMS. Divides heap into regions and performs garbage collection on regions containing most garbage first. Allows flexible resizing of young generation.

Term: Epsilon Garbage Collector
Definition: Do-nothing garbage collector introduced in JDK 11. Handles memory allocation but lacks memory reclamation mechanism. Suitable for ultra-latency-sensitive applications or garbage-free applications.

Term: Shenandoah
Definition: GC introduced in JDK 12, performs garbage collection concurrently with application threads. Can compact live objects and clean garbage while the application is running, making it more CPU intensive.

Term: ZGC (Z Garbage Collector)
Definition: GC introduced in JDK 11, improved in JDK 12, intended for applications requiring low latency and/or large heap sizes. Allows application to continue running during garbage collection operations, providing extremely low pause times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly