What is Garbage Collection?
We already know what is Garbage Collection, one line answer for what is Garbage Collection; "Garbage Collection is the process to reclaim the memory of unreachable objects." One of the most important achievement in java is Garbage Collection with respective to memory management. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. Most of the memory-management issues are resolved by Garbage Collection process. We will discuss about how Garbage Collection really works.
How Garbage Collection Really Works?
Many of us thinking that, Garbage Collection collects all unreachable objects and discards. But really Garbage Collection is doing opposite to our thought. Garbage Collection will track only live objects and rest all will be marked as garbage. As we many of us aware of various memory areas in JVM, one of the most important memory area with respect to developer is Heap area. This Heap area is used for dynamic memory allocation by JVM. In most configurations the operating system allocates the heap in advance to be managed by the JVM while the program is running. An every object creation some portion of memory will be allocated and moves the offset pointer forward.(Figure 1) Next allocation will start with this offset and claims next some portion of array. When an object is no longer referenced, garbage collection will reclaim the memory and reuse it for future objects.
Until this, we are clear that, every object created by developer will stored in heap area which is managed by JVM. As long as objects are referenced, JVM will consider those objects are alive. If objects are unreachable Garbage Collection will reclaim the memory. Here there will be a question araise, if Garbage Collection is tracking only live objects, then what is the root of the tree?
Garbage-Collection Roots (The root of the all the objects)
Every object will have one or more root objects. If application can reach the root object, then whole tree is reachable. But we have a question here, when root objects are reachable?. There are special objects called garbage-collection roots. (Figure 2) These garbage-collection root objects are always reachable.
There are four kinds of GC root objects
- Local variables
- Active Java threads
- Static variables
- JNI References
In simple java application have below GC roots
- Local variables in main method
- The main thread
- Static variables of the main class
Marking and Sweeping Away Garbage
JVM is running an algorithm internally to determine the unreachable objects. This algorithm is called "mark-and-sweep" algorithm. It is a straightforward and two step process- The algorithm traverses all object references, starting with GC root and mark every object found as alive
- All of the heap memory which are all not marked as alive will be reclaimed by Garbage-Collection
Garbage Collection is intended to remove the cause of memory leaks. That is, if there is no reference to object, but object is not deleted. However its possible to have unused objects that still reachable by an application, because developer has forgot to dereference them (Figure 3). Such objects can not be garbage-collected.
No comments:
Post a Comment