<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sankar Saran Pandey on Medium]]></title>
        <description><![CDATA[Stories by Sankar Saran Pandey on Medium]]></description>
        <link>https://medium.com/@sankar.aem?source=rss-086cd9a67dee------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*4yGlve56X9ccZXbV</url>
            <title>Stories by Sankar Saran Pandey on Medium</title>
            <link>https://medium.com/@sankar.aem?source=rss-086cd9a67dee------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 11:20:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sankar.aem/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java Memory Management]]></title>
            <link>https://medium.com/@sankar.aem/java-memory-management-fbf451c9c38e?source=rss-086cd9a67dee------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbf451c9c38e</guid>
            <dc:creator><![CDATA[Sankar Saran Pandey]]></dc:creator>
            <pubDate>Tue, 10 Mar 2026 10:31:47 GMT</pubDate>
            <atom:updated>2026-03-10T10:31:47.951Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Java Memory Management Introduction</strong></blockquote><blockquote><strong>Memory Areas in Java</strong></blockquote><blockquote><strong>Garbage Collection in java</strong></blockquote><blockquote><strong>JVM Memory Management Parameters</strong></blockquote><blockquote><strong>References</strong></blockquote><blockquote><strong>Java Memory Management Introduction</strong></blockquote><p><strong>Java memory management involves the automatic allocation and deallocation of memory through a structured system that includes heap memory for objects, stack memory for method execution, and a method area for class data.</strong></p><p><strong>The garbage collector automatically reclaims memory from unused objects, preventing memory leaks and optimizing resource utilization.</strong></p><p><strong>Developers can configure memory settings through JVM parameters.</strong></p><p><strong>Overall, effective memory management is crucial for building robust and efficient Java applications.</strong></p><h4><strong>Memory Areas in Java</strong></h4><blockquote><strong>Heap Memory</strong></blockquote><p>Main runtime data area for dynamic memory allocation.</p><p><strong>Components:</strong></p><p>•<strong>Young Generation: </strong>Contains newly created objects.</p><p>•<strong>Eden Space: </strong>Initial space for new objects.</p><p>•<strong>Survivor Spaces (S0 &amp; S1): </strong>Store objects that survive garbage collection.</p><p>•<strong>Old Generation: </strong>Holds long-lived objects.</p><blockquote><strong>Stack Memory</strong></blockquote><p>Each thread has its own stack.</p><p><strong>Purpose: </strong>Stores local variables, method parameters, and return addresses.</p><p><strong>Characteristics: </strong>Operates in a last-in, first-out (LIFO) manner, providing fast access for method calls.</p><blockquote><strong>Method Area</strong></blockquote><p>Shared area for class-level data.</p><p><strong>Contents:</strong></p><p>•Class metadata (structure and methods).</p><p>•Static variables.</p><p>•Constant pool (literals and symbolic references).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ft62aaNrNXnMK1pPMiwE3w.png" /></figure><blockquote><strong>Garbage Collection in java</strong></blockquote><p>Garbage collection in Java is the process by which Java programs perform automatic memory management, allocation and deallocation of memory</p><p>When Java programs run on the JVM, objects are created on the heap,</p><p>If, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.</p><blockquote><strong>How Does Garbage Collection works in Java?</strong></blockquote><p>Java garbage collection is an automatic process.</p><p>Automatic 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.</p><p>An in-use object, or a referenced object, means that some part of your program still maintains a referenced to that object.</p><p>An unused or unreferenced object is no longer referenced by any part of your program. So, the memory used by an unreferenced object can be reclaimed.</p><p>The programmer does not need to mark objects to be deleted explicitly. The garbage collection implementation lives in the JVM.</p><blockquote><strong>Types of Activities in Java Garbage Collection</strong></blockquote><p>Two types of garbage collection activity usually happen in Java. These are:</p><p>1.<strong> Minor or incremental Garbage Collection:</strong></p><p>It have occurred when unreachable objects in the young generation heap memory are removed.</p><p><strong>2. Major or Full Garbage Collection:</strong></p><p>It have occurred when the objects that survived the minor garbage collection are copied into the old generation or permanent generation heap memory are removed.</p><p>When compared to the young generation, garbage collection happens less frequently in the old generation.</p><blockquote><strong>Important Concepts Related to Garbage Collection in Java</strong></blockquote><p><strong>Unreachable objects: </strong>An object is unreachable if it doesn’t contain any reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b_16LE-48Ry9kqS8h6Lgqg.png" /></figure><p><strong>Eligibility for garbage collection:</strong></p><p>An object is eligible for GC(garbage collection) if it is unreachable.</p><p>After <em>i = null</em>, integer object 4 in the heap area is suitable for garbage collection in the above image.</p><p><strong>Ways to make an object eligible for Garbage Collector</strong></p><p>•Even the programmer is not responsible for destroying unused objects, but it is highly recommended to make an object unreachable(thus eligible for GC) if it is no longer required.</p><p>•There are generally these ways to make an object eligible for garbage collection.</p><p>•Assign NULL the reference variable</p><p>•Re-assigning the reference variable</p><ul><li>An object created inside the method</li></ul><blockquote><strong>Ways for requesting JVM to run Garbage Collector</strong></blockquote><p>Once we make an object eligible for garbage collection, it may not destroy immediately by the garbage collector. Whenever JVM runs the Garbage Collector program, then only the object will be destroyed. But when JVM runs Garbage Collector, we can not expect.</p><p><strong>We can also request JVM to run Garbage Collector. There are two ways to do it :</strong></p><p>•<strong>Using <em>System.gc()</em> method: </strong>System class contain static method <em>gc()</em> for requesting JVM to run Garbage Collector.</p><p>•<strong>Using <em>Runtime.getRuntime().gc()</em> method:</strong> <a href="https://www.geeksforgeeks.org/java-lang-runtime-class-in-java/">Runtime class</a> allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run Garbage Collector.</p><p>•There is no guarantee that any of the above two methods will run Garbage Collector.</p><ul><li>The call <em>System.gc()</em> is effectively equivalent to the call : <em>Runtime.getRuntime().gc()</em></li></ul><blockquote><strong>Garbage Collector Types</strong></blockquote><p>There are four types of the garbage collector in <a href="https://www.javatpoint.com/java-tutorial">Java</a> that can be used according to the requirement:</p><p>•Serial Garbage Collector</p><p>•Parallel Garbage Collector (default in java 8)</p><p>•Concurrent Mark Sweep (CMS) Garbage Collector</p><ul><li>Garbage First (G1) Garbage Collector (default in java 17)</li></ul><blockquote><strong>JVM Parameters</strong></blockquote><p>The table describes the parameters that can be used to instruct the JVM for GC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3y8aEKSe6UMPvaKDdw_60A.png" /></figure><blockquote><strong>JVM Parameters that affect memory management in the Java Virtual Machine (JVM):</strong></blockquote><p>•<strong>-Xms</strong>: Sets the minimum and initial size of the heap</p><p>•<strong>-Xmx</strong>: Sets the maximum size of the heap</p><p>•<strong>-XX:NewSize:</strong> Sets the size of the young generation heap</p><p>•<strong>-XX:MaxNewSize</strong>: Sets the maximum size of the young generation heap</p><p>•<strong>-XX:MetaspaceSize</strong>: Sets the initial size of Metaspace</p><ul><li><strong>-XX:MaxMetaspaceSize</strong>: Sets the maximum size of Metaspace</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbf451c9c38e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>