Garbage Collection

  • When we work on Java at runtime, a lot of buffer space is used, so when we want to release, unreferenced buffer memory. Garbage collection is performed.
  • It is a process of automatically reclaiming, unused memory by destroying unused objects.
  • In Java garbage collection happens automatically during the lifetime of a program, eliminating the need to deallocate memory and therefore avoiding memory leaks.
  • Java garbage collection is the process by which Java programs perform automatic memory management.
  • When Java programs run ,the JVM objects are created in the heap space, Which is the portion of member dedicated to the program.
  • To make an object candidate of Java garbage collection.
    • Make the reference null.
    • Assign a reference to another object.
    • Using an anonymous object
      • My method(new Class());
  • Three main phases of garbage collector are
    • Mark the object as dead or alive.
    • Sweep dead object.
    • Compact remaining objects.
  • Generational garbage collection
    • As more and more objects are allocated, the list of objects grows, Leading to longer garbage collector times.
    • Several analysis of application have shown that most objects have a very short lifespan.
    • To benefit from these findings, the heap memory area in the JVM was divided into three sections
      • Young Generation
        • Eden space
        • If the object is in this space survive one garbage collection cycle, then they are move to the survivor space.
        • When the objects are collected from young generation, it is a minor GC event.
        • A minor GC event is performed when the Eden space is filled with objects, either dead or alive.
        • All the dead objects are deleted, and all the all alive objects are moved to one of these survivors spaces.
        • At any time, one of the survivor spaces is always empty.
        • When the survival objects reach a certain Threshold of moving around The survivor Spaces, They are moved to the old generation.
      • Old generation
        • Old generation contains objects that have remained in the survivor space for a long time.
        • When objects are garbage collected from the old generation, it is a major GC event.
      • Permanent generation space
        • Meta data of classes and methods are stored in permanent generation. Space.
        • Since Java eight, the Meta space memory Space replace the permanent generation space.
        • This space can automatically be resized. 
        • This avoid the problem of Applications running out of memory due to the limited size of permanent generation size, space of heap.
        • The meta space can be garbage, collected, and class no longer being used can be automatically cleaned when meta space is full.
  • Types of garbage collectors
    • Serial garbage collector
      • Simplest implementation of garbage collector and is designed for small applications running on single threaded environments.
      • All garbage collection events are performed, serially and in one thread.
      • Garbage collection thread, stop the application while progress leading to “ Stop the word” Events.
    • Parallel garbage collector
      • Multiple threads are used for minor garbage collection in the young generation and a single third is used for major garbage collection in the old generation.
      • Used for application with medium size to large size, data sets and runs on multi-threaded hardware.
      • It also causes a” Stop the world event” And the application freezes
    • Concurrent mark sweep(CMS)
      • Multiple threats are used for minor and major garbage collection.
      • CMS runs concurrently alongside application process to minimise “ Stop the world” event.
      • The CMS Collector uses More CPU, then other GC’s So if you can allocate more CPU for better performance, then CMS is a better choice, then parallel garbage collector.
    • Garbage First(G1GC)
      • G1GC was designed for multi applications that have a large heat size available under the hood. It works quite differently, compare to older garbage collectors.
      • It is Parallel and concurrent like CMS.
      • G1GC Is also generational, but it does not have separate regions for young and old generations. Instead, each generation is a set of regions which allows resizing in a flexible way.
    • Eplison GC
      • Eplison Was released as a part of JDK 11.
      • It is a do nothing garbage collector, it handles memory allocation, but does not reclaim any actual memory. Once the available, Java is exhausted, the JVM shuts down.
      • It can be used for ultra Latency, sensitive applications where developers, know exactly the needed, memory, or even have completely garbage free applications.
    • Shenandoah GC
      • It was released as a part of Java 12.
      • Shenandooh’s Key advantage over G1 is That it does more of its garbage collection cycle work, concurrently with the Application threads.
      • G1GC Can evacuate its heap regions only when the application is paused, while Shendooh Can we locate objects concurrently with the application.
      • Shenandooh Can compact live objects, clean garbage, and release RAM back to the OS almost immediately after it detaches free memory
      • Shenandoah Is more CPU intensive.
    • ZGC
      • Was released as a part of JDK 11 and improved in JDK 12.
      • ZGC is intended for applications which require low latency. All use a very high heap.
      • ZGC Allows a Java application to continue running while it performs all garbage collection operations.
      • The primary goal are low latency, Scalability, and ease of use.
      • ZGC Wings of significant improvement over the other traditional garbage collectors by providing extremely low pause time.
  • Advantages of garbage collectors
    • It makes our code simple.
      • You just stop using an object in your code, and the memory it is using will be automatically reclaimed at some point.
    • It makes Java memory efficient.
      • The GCC removes the unreferenced objects from heap memory, freeing it to accommodate new objects.

No comments:

Post a Comment