How Garbage Collection Works in Java?In Java, garbage collection is the process of managing memory, automatically. It finds the unused objects (that are no longer used by the program) and delete or remove them to free up the memory. The garbage collection mechanism uses several GC algorithms. The most popular algorithm that is used is Mark and Sweep. In this section, we will learn when an object becomes eligible to garbage collection, types of garbage collection, and Mark and Sweep algorithm. Along with this, we will understand how garbage collection works in Java? Garbage Collector OverviewWhen a program executes in Java, it uses memory in different ways. The heap is a part of memory where objects live. It's the only part of memory that involved in the garbage collection process. It is also known as garbage collectible heap. All the garbage collection makes sure that the heap has as much free space as possible. The function of the garbage collector is to find and delete the objects that cannot be reached. Important Points About Garbage Collector
Object AllocationWhen an object allocates, the JRockit JVM checks the size of the object. It distinguishes between small and large objects. The small and large size depends on the JVM version, heap size, garbage collection strategy, and platform used. The size of an object is usually between 2 to 128 KB. The small objects are stored in Thread Local Area (TLA) which is a free chunk of the heap. TLA does not synchronize with other threads. When TLA becomes full, it requests for new TLA. On the other hand, large objects that do not fit inside the TLA directly allocated into the heap. If a thread is using the young space, it directly stored in the old space. The large object requires more synchronization between the threads. When an Object becomes eligible for Garbage Collection?An object become eligible if it is not used by any program or thread or any static references or its references is null. If two objects having reference (cyclic reference) of each other and does not have any live reference then both objects collected by the garbage collector. There are some other cases when an object become eligible for garbage collection:
What does Java Garbage Collector?JVM controls the garbage collector. JVM decides when to perform the garbage collection. You can also request to the JVM to run the garbage collector. But there is no guarantee under any conditions that the JVM will comply. JVM runs the garbage collector if it senses that memory is running low. When Java program request for the garbage collector, the JVM usually grants the request in short order. It does not make sure that the requests accept. The point to understand is that "when an object becomes eligible for garbage collection?" Every Java program has more than one thread. Each thread has its execution stack. There is a thread to run in Java program that is a main() method. Now we can say that an object is eligible for garbage collection when no live thread can access it. The garbage collector considers that object as eligible for deletion. If a program has a reference variable that refers to an object, that reference variable available to live thread, this object is called reachable. Here a question arises that "Can a Java application run out of memory?" The answer is yes. The garbage collection system attempts to objects from the memory when they are not in use. Though, if you are maintaining many live objects, garbage collection does not guarantee that there is enough memory. Only available memory will be managed effectively. Types of Garbage CollectionThere are five types of garbage collection are as follows:
Mark and Sweep AlgorithmJRockit JVM uses the mark and sweep algorithm for performing the garbage collection. It contains two phases, the mark phase, and the sweep phase. Mark Phase: Objects that are accessible from the threads, native handles, and other GC root sources are marked as live. Every object tree has more than one root object. GC root is always reachable. So, any object that has a garbage collection root at its root. It identifies and marks all objects that are in use, and the remaining can be considered garbage. Sweep Phase: In this phase, the heap is traversed to find the gap between the live objects. These gaps are recorded in the free list and are available for new object allocation. There are two improved versions of mark and sweep:
Concurrent Mark and SweepIt allows the threads to continue running during a large portion of the garbage collection. There are following types of marking:
Parallel Mark and SweepIt uses all available CPU in the system for performing the garbage collection as fast as possible. It is also called the parallel garbage collector. Threads do not execute when the parallel garbage collection executes. Pros of Mark and Sweep
Cons of Mark and Sweep
Next TopicJava Tutorial
|