Recent Posts

Friday, 2 September 2016

Garbage Collection


What is Garbage Collection
     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 unreferenced objects. Garbage Collection is a way to destroy the unused objects. To achieve garbase 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 
1. 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.

2. Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.

3. 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.

4. Garbage Collection in Java is carried by a daemon thread called Garbage Collector.

5. 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.

6. 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.

7. 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.

8. 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:

1. All references to that object explicitly set to null e.g. object = null

2. The object is created inside a block and reference goes out scope once control exit that block.

3. 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.

Heap Generations for Garbage Collection in Java
      Java objects are created in Heap and Heap is divided into three parts or generations for the sake of garbage collection in Java, these are called as Young  generation, Tenured or Old Generation and Perm Area of the heap. 

1. Young Generation
     Young generation is the place where all the new objects are created. When young generation is filled, garbage collection is performed. This garbage collection is called Minor GC. Young Generation is divided into three parts – Eden Memory, Survivor 1 and Survivor 2 space. When an object first created in heap its gets created in new generation inside Eden space and after subsequent minor garbage collection if an object survives its gets moved to survivor 1 and then survivor 2 before major garbage collection moved that object to old or tenured generation. Important Points about Young Generation Spaces:
1. Most of the newly created objects are located in the Eden memory space.

2. When Eden space is filled with objects, Minor GC is performed and all the survivor objects are moved to one of the survivor spaces.

3. Minor GC also checks the survivor objects and move them to the other survivor space. So at a time, one of the survivor space is always empty.

4. Objects that are survived after many cycles of GC, are moved to the Old generation memory space. Usually it’s done by setting a threshold for the age of the young generation objects before they become eligible to promote to Old generation.

2. Old Generation
     Old Generation memory contains the objects that are long lived and survived after many rounds of Minor GC. Usually garbage collection is performed in Old Generation memory when it’s full. Old Generation Garbage Collection is called Major GC and usually takes longer time.

3. Permanent Generation
     Permanent Generation or PermGen is a non-heap memory area here the Class Loading happens and the JVM allocates spaces for classes, class meta data, stores class level details, loading and unloading classes (e.g. JSPs), methods, String pool. java methods and the reference Objects here.
Perm Gen contains the metadata of the classes.
1. Methods of a class (including the bytecodes)

2. Names of the classes (in the form of an object that points to a string also in the permanent generation)

3. Constant pool(e.g String pool) information (data read from the class file).

4. Object arrays and type arrays associated with a class (e.g., an object array containing references to methods).

5. Internal objects created by the JVM (java/lang/Object or java/lang/exception for instance.

    The PermGen is independent from the Heap Area. you  can resize it according to the requirement using -XX:MaxPermSize and -XX:PermSize  JVM Options. The Garbage collection happens in this area of JVM Memory as well. The Garbage collection in this area is called as “Class GC”. We can disable the Class Garbage Collection using the JVM Option -noclassgc. if  ”-noclassgc” Java Option is added while starting the Server.it also holds all the reflective data of the virtual machine itself, stores class level details, loading and unloading classes (e.g. JSPs), methods, String pool.

     Java has four types of garbage collectors,
1. Serial Garbage Collector
2. Parallel Garbage Collector
3. CMS Garbage Collector
4. G1 Garbage Collector

1. Serial Garbage Collector
     Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs. Turn on the -XX:+UseSerialGC JVM argument to use the serial garbage collector.

2. Parallel Garbage Collector
     Parallel garbage collector is also called as throughput collector. It is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection. Turn on the -XX:+UseParallelGC JVM argument to use the parallel garbage collector.

3. CMS Garbage Collector
     Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads in the following two scenarios only,
1. While marking the referenced objects in the tenured generation space.
2. If there is a change in heap memory in parallel while doing the garbage collection.

     In comparison with parallel garbage collector, CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector. Turn on the XX:+USeParNewGC JVM argument to use the CMS garbage collector.

4. G1 Garbage Collector
     G1 garbage collector is used for large heap memory areas. It separates the heap memory into regions and does collection within them in parallel. G1 also does compacts the free heap space on the go just after reclaiming the memory. But CMS garbage collector compacts the memory on stop the world (STW) situations. G1 collector prioritizes the region based on most garbage first. Turn on the –XX:+UseG1GC JVM argument to use the G1 garbage collector.


No comments:

Post a Comment