{"id":52592,"date":"2018-01-20T10:10:20","date_gmt":"2018-01-20T04:40:20","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=52592"},"modified":"2018-01-20T10:10:20","modified_gmt":"2018-01-20T04:40:20","slug":"java-garbage-collection-an-overview","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/java-garbage-collection-an-overview\/","title":{"rendered":"Java Garbage Collection &#8211; An Overview"},"content":{"rendered":"<p>Garbage Collection is a process to identify and delete the objects from Heap memory which are not in use. GC frees the space after removing unreferenced objects.<\/p>\n<p><span style=\"font-weight: 400\">The event in which Garbage Collectors are doing their job is called<\/span><b> &#8220;Stop the world&#8221;<\/b><span style=\"font-weight: 400\"> event which means all of your application threads are put on hold until the garbage is collected.<\/span><\/p>\n<h2><span style=\"color: #000000\"><b>How Garbage Collector works<\/b><\/span><\/h2>\n<p><span style=\"font-weight: 400\">A Java Virtual Machine (JVM) is a software implementation of a physical machine. There are various implementations of JVM. Here, we will be talking about Oracle HotSpot JVM. The basic process of Hotspot JVM Garbage collector completes in two phases:<\/span><\/p>\n<h3><span style=\"color: #000000\"><b><i>Phase 1. Marking<\/i><\/b><\/span><\/h3>\n<p>This phase is called marking phase in which GC identifies which objects are in use or which are not. All objects are scanned in the marking phase to make this determination.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-52599 size-full\" title=\"Marking-Phase\" src=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Marking-Phase.png\" alt=\"Marking-Phase\" width=\"588\" height=\"352\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Marking-Phase.png 588w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/Marking-Phase-300x179.png 300w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><\/p>\n<h3><b><em><span style=\"color: #000000\">Phase 2. Deletion<\/span><\/em><\/b><\/h3>\n<p>In Deletion phase, the marked object is deleted and the memory is released. Deletion of the unreferenced objects can be done in two ways:<\/p>\n<ul>\n<li><strong><i>Normal Deletion:\u00a0\u00a0<\/i><\/strong>In this phase, all unused objects will be removed and memory allocator has pointers to free space where a new object can be allocated.<\/li>\n<\/ul>\n<ul>\n<li><strong><i>Deletion and Compaction:\u00a0<\/i><\/strong>As you see in normal deletion there are free blocks between referenced objects. To further improve performance, in addition to deleting unreferenced objects, remaining referenced object will be compact<i><\/i><strong><i><strong>.<\/strong><\/i><\/strong><\/li>\n<\/ul>\n<h2><span style=\"color: #000000\"><b>Why Heap divided into Generations<\/b><\/span><\/h2>\n<p><span style=\"font-weight: 400\">It is a cumbersome process to scan all of the objects from a whole heap and further mark and compact them.\u00a0The list of the object grows\u00a0gradually which leads\u00a0to longer garbage collection time\u00a0as\u00a0more and more objects are allocated with\u00a0time.<\/span><\/p>\n<p><span style=\"font-weight: 400\">In General Applications most of the objects are short-lived. Fewer and fewer objects remain allocated over time. <\/span><\/p>\n<p><span style=\"font-weight: 400\">That\u2019s why to enhance the performance of the JVM, Heap is broken up into smaller parts called <strong>generations\u00a0<\/strong>and JVM performs GC in these generations when the memory is about to fill up.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-52602 size-full\" title=\"Heap-Structure\" src=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Heap-Structure.png\" alt=\"Heap-Structure\" width=\"690\" height=\"327\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Heap-Structure.png 690w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/Heap-Structure-300x142.png 300w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/Heap-Structure-624x295.png 624w\" sizes=\"(max-width: 690px) 100vw, 690px\" \/><\/p>\n<h2><span style=\"color: #000000\"><b>Generational Process of Garbage Collection<\/b><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Now, when you know why heap is divided into generations, it&#8217;s time to look into how these generations would interact.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400\"> New objects are allocated in Eden Space of <strong>Young Generation<\/strong>. Both Survivor Spaces\u00a0are empty in starting.<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-52626 size-full\" title=\"object-allocation-in-yg\" src=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/object-allocation-in-yg.png\" alt=\"object-allocation-in-yg\" width=\"514\" height=\"355\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/object-allocation-in-yg.png 514w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/object-allocation-in-yg-300x207.png 300w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/p>\n<ul>\n<li><strong><span style=\"font-weight: 400\">A minor garbage collection will trigger once the Eden space fills up.<\/span><\/strong><\/li>\n<li><span style=\"font-weight: 400\">Referenced objects are moved to the S0 survivor space and Eden Space will be cleared and all unreferenced objects will be deleted.<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-52605 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/copying-objects.png\" alt=\"copying-objects\" width=\"589\" height=\"379\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/copying-objects.png 589w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/copying-objects-300x193.png 300w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">It will happen again to Eden space when next time GC will be triggered. However, in this case, all referenced objects are moved to S1 survivor space. In addition, objects from the last minor GC on the S0 survivor space have their age incremented and get moved to S1. Now both Eden and S0 will be cleared, and this process will repeat every time when GC is triggered. On every GC triggered, survivor spaces will be switched and object&#8217;s age will be incremented.<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-52606 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Object-Aging.png\" alt=\"Object-Aging\" width=\"591\" height=\"410\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2018\/01\/Object-Aging.png 591w, \/blog\/wp-ttn-blog\/uploads\/2018\/01\/Object-Aging-300x208.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/p>\n<ul>\n<li><span style=\"font-weight: 400\">Once the objects reach a certain age threshold, they are promoted from young generation to old generation. So, this pretty much describes how objects promotion takes place.<\/span><\/li>\n<li><strong><span style=\"font-weight: 400\">The major GC will be triggered once the old generation completely fills up.<\/span><\/strong><\/li>\n<\/ul>\n<h2><span style=\"color: #000000\"><b>Available Garbage collectors in Hotspot JVM<\/b><\/span><\/h2>\n<ul>\n<li><b style=\"font-size: 1rem\">Serial Garbage Collector:\u00a0<\/b><strong><span style=\"font-weight: 400\">Serial GC designed for the single-threaded environments. It uses just a single thread to collect garbage.<\/span><span style=\"font-weight: 400\">\u00a0It is <\/span><\/strong><span style=\"font-weight: 400\">best<\/span><strong><span style=\"font-weight: 400\"> suited for simple command-line programs. Though it can be used on multiprocessors for applications with small data sets.<\/span><\/strong><\/li>\n<\/ul>\n<ul>\n<li><b>Parallel Garbage Collector:\u00a0<\/b>Unlike Serial GC it uses multiple threads for garbage collection. It is a default collector of JVM and it is also called the <strong>Throughput garbage collector.<\/strong><\/li>\n<\/ul>\n<ul>\n<li><b>CMS Garbage Collector:\u00a0<\/b>CMS uses multiple threads at the same time to scan the heap memory and mark in the available for eviction and then sweep the marked instances.<\/li>\n<\/ul>\n<ul>\n<li><b>G1 Garbage Collector:\u00a0<\/b>G1 Garbage collector is also called the Garbage First. It is available\u00a0since Java 7 and its long-term goal is to replace the CMS collector. The G1 collector is a parallel, concurrent, <span style=\"font-weight: 400\">and incrementally compacting low-pause garbage collector.<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000\"><strong>Points to Remember <\/strong>for tuning the Generations to optimize the performance:<\/span><\/p>\n<ul>\n<li>Based on certain hardware and operating system configurations one of these Garbage Collector is selected by default.\u00a0The purpose of various garbage collectors are available is to choose appropriate according to the need of your application.<\/li>\n<li>\u00a0To explicitly enable the particular GC we can use given (<em><strong>-XX:+UseSerialGC<\/strong><\/em>, &#8211;<em><strong>XX:+UseSerialGC<\/strong><\/em>, &#8211;<strong><em>XX:+UseParallelGC<\/em><\/strong>,<em><strong> -XX:+UseG1GC<\/strong><\/em>) VM options.<\/li>\n<li>Mainly, two components of JVM are focused on, when tuning performance The <strong>Heap<\/strong> and the\u00a0<strong>Garbage Collector.<\/strong><\/li>\n<li>Young Generation is the first place to optimize the performance,\u00a0 to define the size of YG we can use\u00a0\u00a0<strong><em>-XX<\/em>:NewSize<\/strong> and <strong><em>-XX<\/em>:MaxNewSize<\/strong>\u00a0VM options.<\/li>\n<li><strong>-Xmx\/2<\/strong> (<strong>-Xmx<\/strong> to set the maximum heap size) is an upper bound for <strong>-XX:MaxNewSize<\/strong>. This is for stability reason. It is not allowed to choose a young generation&#8217;s size larger than the old generation.<\/li>\n<li>It is also possible to specify the young generation size in relation to the size of the old generation using <strong>-XX:NewRation<\/strong>. For example, if we set<em><strong>\u00a0<\/strong><strong>-XX: NewRatio=3<\/strong><\/em>, it means the\u00a0old generation will be 3 times larger then YG.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Garbage Collection is a process to identify and delete the objects from Heap memory which are not in use. GC frees the space after removing unreferenced objects. The event in which Garbage Collectors are doing their job is called &#8220;Stop the world&#8221; event which means all of your application threads are put on hold until [&hellip;]<\/p>\n","protected":false},"author":1195,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":69},"categories":[446,1],"tags":[740,4781,4844,3836],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/52592"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1195"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=52592"}],"version-history":[{"count":52,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/52592\/revisions"}],"predecessor-version":[{"id":52679,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/52592\/revisions\/52679"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=52592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=52592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=52592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}