Garbage Collection in java

Garbage Collection

let me explain to you with real-time example. Usually we go to some restaurant in free time in evening with family. At hotel waiter clean the table and pick the garbage from the table and make it available for new customer.

Assume you are Object , restaurant is JVM and waiter is thread who clean the table and available for new customer.

Now with real -time example we have understand What is Garbagee Collector, In Computer language Garbage Collector is a Daemon thread that keeps running in the background. Basically, it frees up the heap memory by destroying the unreachable objects.

Types of Garbage Collectors

The Serial Collector

Parallel/Throughput GC

It is best suited if applications that can handle such pauses, and try to optimize CPU overhead caused by the collector

The CMS Collector

This algorithm Stop the World in two places:-

  • when initializing the initial marking of roots objects in the old generation that are reachable from thread entry points or static variables
  • when the application has changed the state of the heap while the algorithm was running concurrently, forcing it to go back and do some final touches to make sure it has the right objects marked.

This collector may face promotion failures. If some objects from young generation are to be moved to the old generation, and the collector did not have enough time to make space in the old generation space, a promotion failure will occur. To overcome this usually provide more heap size to the old generation space.

G1 collector

This strategy reduced the chance of the heap being depleted before background threads have finished scanning for unused objects, in which case the collector will have to stop the application which will result in a Stop The World(STW) collection. The G1 also has another advantage that is that it compacts the heap on-the-go, something the CMS collector only does during full STW collections.

Large heaps have been a fairly contentious area over the past few years with many developers moving away from the single JVM per machine model to more micro-service, componentized architectures with multiple JVMs per machine. This has been driven by many factors including the desire to isolate different application parts, simplifying deployment and avoiding the cost which would usually come with reloading application classes into memory (something which has actually been improved in Java 8).

Java 8 PermGen and Metaspace

Most of the allocations for the class metadata are made out of the native memory. There is a new flag MaxMetaspaceSize, to limit the amount of memory used for class metadata. If we do not specify the value for this, the Metaspace re-sizes at runtime as per the demand of the running application.

Metaspace garbage collection is triggered when the class metadata usage reaches MaxMetaspaceSize limit. Excessive Metaspace garbage collection may be a symptom of classes, classloaders memory leak or inadequate sizing for our application.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store