Wednesday, October 26, 2016

Java Garbage Collection

Garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects.


unused objects are considered to be any instances that cannot be reached by a live thread or for circularly referenced instances that cannot be reached by any other instances.
The basic process are :
  1. Marking : garbage collector identifies which objects are used and which are unused.
  2. Deletion: remove unused object and pointer to free space.
  3. Compacting : To improve performance move used object together so that there is no gap among them.   
Having to mark, delete and compact all the objects in a JVM is inefficient. empirical analysis of applications has shown that most objects are short lived and fewer objects remain allocated over time hence JVM Heap is broken into smaller parts as Young Generation, Tenured/ Old generation, Permanent Generation(removed in java 8).

Young Generation: This part of the heap is further divided into Eden Space, First survivor space, and Second survivor space.
Old Generation: long survived object are stored after minor GC.  
Permanent Generation: contain metadata of class and methods  required by JVM.

Few Important Terms:
Minor GC: minor GC triggered when JVM unable to allocate memory for new object. Generally when Eden space is full.
Major GC: happens for the Old Generation or aged Objects.
Stop The World : All minor/major garbage collections are "Stop the World" events means that all application threads are stopped until the operation completes.

Generational Garbage Collection Process:
  1. New objects are allocated to the eden space.
  2. When eden space is full minor GC triggered referenced object moved to first survivor space and eden space is cleared. During each minor GC aged object from first survivor space moved to second survivor space.
  3. As minor GCs continued age of the survived objects incremented and at certain threshold they moved to Old generation space. a major GC will be performed on the old generation which cleans up and compacts that space.
Garbage Collectors:
The Java HotSpot VM includes three different types of collectors, each with different performance characteristics.

  1. Serial Collector : Uses a single thread to perform GC hence it cannot take advantage of multiprocessor useful for application with small data sets. The serial collector is selected by default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseSerialGC.

  1. Parallel Collector: known as the throughput collector performs collections in parallel, which can significantly reduce garbage collection overhead and intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multithreaded hardware. default on certain hardware and operating system configurations, or can be explicitly enabled with the option -XX:+UseParallelGC.

  1. Concurrent collector :  Java Hotspot VM has two mostly concurrent collectors are Concurrent Mark Sweep(CMS Collector) and Garbage-First Garbage Collector(java 8).

Characterize the actual different primary garbage collectors:

Young generation collectors

  1. Serial collector (enabled with -XX:+UseSerialGC)
  2. PS Scavenge (enabled with -XX:+UseParallelGC)
  3. ParNew (enabled with -XX:+UseParNewGC)
  4. G1 Young Generation (enabled with -XX:+UseG1GC)

Old generation collectors

  1. MarkSweepCompact (enabled with -XX:+UseSerialGC)
  2. PS MarkSweep (enabled with -XX:+UseParallelOldGC)
  3. ConcurrentMarkSweep (enabled with -XX:+UseConcMarkSweepGC)
  4. G1 Mixed Generation (enabled with -XX:+UseG1GC)

All of the garbage collection algorithms except ConcurrentMarkSweep are stop-the-world, it tries to do most of it's work in the background and minimize the pause time, but it also has a stop-the-world phase and can fail into the MarkSweepCompact which is fully stop-the-world.

Reference :
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

1 comment:

  1. I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. pittsburg california junk pickup

    ReplyDelete

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....