Garbage Collection In Java
JVM with its built-in garbage collection functionality is a boon to Java developer. It allows developer to program without worrying much about memory management, because Java Garbage Collector will take care of it. This is unlike most programming languages like C, C++ etc. where the programmer need to handle memory management as well.
Garbage Collector in Java automatically reclaims the allocated memory for reuse. It traces the live objects and keeps garbaging unreferenced objects.
Heap and Memory Leak
Java objects are created in the Heap irrespective of their scope. Class variables and static members are created in method area of Java memory space. Both heap and method area is shared between different threads. In this article, we will talk about Heap Area which is used for dynamic memory allocations. Most of the Operating Systems allocate the heap in advance to be managed by JVM while the program is running.
When an object is created, JVM allocates a portion of heap space and moves the offset pointer, so the next allocation will be at this offset. When the object is no more referred, therefore unreachable by the application code, Garbage Collector retrieves the underlying memory and reuses it for future allocation.
There are possibilities to have unused objects that are still reachable by application code which the developer might have forgotten to dereference. Such objects are neither used nor can be garbage collected which leads to a situation called memory leak.
Garbage Collection Roots
Garbage Collector starts its work by looking at set of references called as 'Garbage Collection roots' which are always reachable by JVM and contain references to objects created by the underlying application. Every object in heap must be refered by atleast one 'GC root' object. When garbage collection starts, it marks these objects as live object and then looks at any objects that they have reference. And so it continues till it reaches top level and no further objects are refered. The GC roots includes system classes (loaded by bootstrap/system), local variables, main thread, etc.
JVM uses a Garbage Collector called Tracing Collector which pause all the surrounding root objects(objects referenced directly by running threads), their references and every objects it sees on its way.
Generational Garbage Collector
Java implements generational Garbage Collector based upon the Generational Hypothesis which states that the majority of objects that are created are quickly discarded, and objects that are not quickly collected are likely to be around for a while. Based on this, Java partitions memory area into different generations and operates differently upon them.
The different generations are:
- Young Generation
- Tenured Generation
- Permanent Generation
It has two sub-generations: Eden Space and Survivor Space. Survivor Space has two partitions S0 and S1.
Eden Space: When objects are created, they first reside in Eden Space of heap memory area. Here the Garbage Collector discards the unreferenced objects and migrates the live objects to one of the Survivor Space.
Survivor Space: Any one of the Survivor Space is in use at any given point of time. Objects that are alive in Eden Space are moved to Survivor Space during garbage collection.
The Young Generation is handled by a tracing & copying collector called as "Parallel New Collector". When Eden Space fills up, Garbage Collector pauses the world around it and then traces through the objects in the young generation. Objects those are marked alive are moved to Survivor space. All unreferenced object in Eden Space will be discarded and it will now be ready to be filled up with new objects. The next garbage collection cycle will use the other Survivor space.
This space is also called as 'Old Generation'. Older objects with longer lifetimes are moved over from the survivor space into this area. When this space is filled up, full garbage collection is performed. JVM throws 'OutOfMemoryError - Java Heap Space' when this space is overflowed.
Old Generation is the last phase of Garbage Collection Process. 'Major Garbage Collector' is used to scans this heap space. If instances are dereferenced, they are discarded otherwise continue to stay in the Old Generation.
Also called as 'Perm Gen Space'. It is not a part of the Java Heap memory. It contains metadata required by the JVM to describe the classes and methods used in the application. It is populated at runtime based on the classes used by the application. These objects are GC in a 'full garbage collection'.
The concept of condition beans enables Spring to restrict the creation of any bean depending on the evaluation of a condition. These beans get created only when a preset condition is evaluated as true