Garbage collection is one of the famous question in most of the interviews and the most common answers will be about high level garbage collection and finalize() method. Here we will dig a little more on this term.
Garbage Collection – In other words, it is well suits for the term Memory management in Java.
Memory management is the process of recognizing when allocated objects are no longer needed, de-allocating
(freeing) the memory used by such objects, and making it available for subsequent allocations.
Before going to dive in garbage collection, we will see some bits and pieces on memory management. In programming world, memory management is broadly classified into two types. One is Explicit Memory Management and the other one is Automatic Memory Management.
Explicit Memory Management – In some programming, memory management is programmer’s responsibility. The programmer needs to perform space allocation and de-allocation for objects. The complexity of this task leads the developer to make many common errors that will cause major impact in applications. As a result, it swollen the developer time to debug the application.
The other fact is, explicit memory management brings two kind of issues.
1. Dangling References
2. Space leaks
Dangling references are, de-allocating the space occupied by an object to which some other objects still has a reference.
Space leaks are, not releasing the space occupied by an object even the object is no longer referenced.
To get rid of from these kind of issues, Java offers automatic memory management which means the JVM does the memory management on behalf of the programmer. The only thing is, we need to tune it for better performances in required. For smaller applications, the default settings itself enough for greater performance results.
What Garbage Collection Does
Garbage collector is responsible for,
1. Allocating memory
2. Ensuring that any referenced objects remain in memory
3. Freeing memory used by objects that are no longer reachable from references in executing code.
What happens when creating an object
We all know that objects are stored in heap memory. Here we see how the heap memory is structured for object handling. The heap memory is divided into three parts (called as generations).
1. Young Generation
2. Tenured or Old Generation
3. Permanent Generation
When creating an object, the garbage collector allots space for it in heap memory (normally in young generation). If memory is filled, then GC initiated by JVM to cleanup the space. In this process, it iterates the live objects and free the space occupied by objects that doesn’t have references.
The objects which survive in GC cycle (live objects) will be moved to Tenured or Old Generation after certain number of cycles. These are considered as long living objects in old generation.
If memory is filled in both young generation and old generation, then JVM initiates the full garbage collection. This will remove the de-referenced objects in both the generations (young and old generation). If both the generations has live objects and there is no further space, then you get possibly Out Of Memory error.
Permanent generation holds the objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves When there is no space to store such a classes and methods, you get PermGen Space issue.
Most of objects are initially allocated in young generation only. When garbage collection occurs, the objects no longer referenced in young generation will be removed in quick time because it uses the efficient collection algorithm designed specifically for young generation.
The young generation consists of an area called Eden plus two smaller survivor spaces. As I told, most objects are initially allocated in Eden. The objects which are survived at-least one GC cycle, will be moved to survivor spaces. In other words, the survivor spaces holds the objects that have survived at least one young generation collection and have thus been given additional chances to die before being moved to “Old Generation”.
Young generation collections are known as minor collection and hence the time it takes for GC will be minimal.
More to follow on garbage collector algorithms in my next article…