Garbage Collection Introduction
Memory management is one of the most crucial part for any programming language. In the Java programming language, dynamic memory allocation of objects is achieved using the new operator. Once an object is created, it uses some memory and the memory remains allocated till there are references for the use of the object. When there are no references for an object, it is assumed to be no longer needed and the memory occupied by the object can be reclaimed. There is no explicit need to destroy an object as java handles the de-allocation automatically.
In general, garbage means un-referenced objects. Garbage Collection is a way to destroy the unused objects. To achieve garbage collection, we are using free() function in C language and delete() in C++. But, in java it is performed automatically. So, java provides better memory management.
Garbage collection works by employing several GC algorithm e.g. Mark and Sweep. There are different kinds of garbage collector available in Java to collect different area of heap memory e.g. you have serial, parallel and concurrent garbage collector in Java.
A new collector called G1 (Garbage first) are also introduced in JDK 1.7. The first step to learning about GC is to understand when an object becomes eligible to garbage collection? Since JVM provides memory management, Java developers only care about creating an object, they don’t care about cleaning up, that is done by the garbage collector, but it can only collect objects which have no live strong reference or it’s not reachable from any thread.
Memory leak in Java is a situation where some objects are not used by the application any more, but GC fails to recognize them as unused.
Important points about Java Garbage Collection
- Objects are created on the heap in Java irrespective of their scope e.g. local or member variable. while it’s worth noting that class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread.
- Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.
- Garbage collection relieves Java programmer from memory management which is an essential part of C++ programming and gives more time to focus on business logic.
- Garbage Collection in Java is carried by a daemon thread called Garbage Collector.
- Before removing an object from memory garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any sort of cleanup required.
- Java programmer can not force garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.
- There are methods like System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
- If there is no memory space for creating a new object in Heap, then JVM throws ava.lang.OutOfMemoryError heap space.
When an Object becomes Eligible for Garbage Collection
An object becomes eligible for Garbage collection if it’s not reachable from any live threads or by any static references. In other words, you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as the reference so if object A has a reference to object B and object B has a reference to Object A and they don’t have any other live reference then both Objects A and B will be eligible for Garbage collection. Generally, an object becomes eligible for garbage collection in Java on following cases:
- All references to that object explicitly set to null e.g. object = null
- The object is created inside a block and reference goes out scope once control exit that block.
- Parent object set to null if an object holds the reference to another object and when you set container object’s reference null, child or contained object automatically becomes eligible for garbage collection.