Sitemap
Thameem Ansari

Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com

Java Memory Management

4 min readMar 20, 2022

--

Press enter or click to view image in full size
Java Memory Management

Introduction

In this article we will be discussing about Heap and Stack memory in java and what is the life of Objects in our application with examples.

To run an application in an optimal way, JVM divides memory into stack and heap memory. Whenever we declare new variables and objects, call a new method, declare a String, or perform similar operations, JVM designates memory to these operations from either Stack or Heap Memory.

Heap Memory

There are two parts inside the Heap area called Young Generation and Old Generation. There are 3 parts inside the Young generation area called Eden and two survivor( Survivor1, Survivor2 ) memories.

In the heap area objects, JRE Classes will be stored. The Garbage Collector will run only on Heap Memory. ( GC will destroy objects which don’t has references and frees memories )

Get Thameem Ansari’s stories in your inbox

Join Medium for free to get updates from this writer.

Memory Pool — This stays in the Heap area. In the memory pool immutable objects, the String pool will be stored.

  1. When Eden memory completes( full ), Minor Garbage Collector( Minor GC ) will perform and Minor GC will move old memory parts(variables) to Survivor memories.
  2. Then after some time, the Young generation may be filled, Then Old objects will be moved to the Old Generation.
  3. Then Old Generation will be filled and there is no space available in the Heap area. Then Major Garbage Collector( Major GC ) will be performed. This Major GC will take some longer time than Minor GC.

Minor GC always performs in the Young Generation and the major GC always performs in the Old Generation.

Stack Memory

This is used to execute the threads. This contains methods-specific values local variables , object references and method blocks. This uses the Last In First Out( LIFO ) way to free the memory(which means the newest/latest memory locations will be destroyed).

  • Local variables — local variables are variables that define in a method, including method parameters. Java Stack memory only contains the byte, short, char, int, long, float and double(primitives) local variables. If we create an object ( Ex: String ) as a local variable that object is stored in the Heap memory area, but the object reference stays in the Stack memory area.
  • Object Reference — variable names which point to objects in the Heap Memory. In the below example “my” is an object reference.
MyClass my = new MyClass();
  • Method Block— This is just a name for methods when talking about Java Memory Management. In the below example “myMethod” will be a method block. When after executing a method that memory location will be freed and may be located for another method block.
public void myMethod() {
int i = 0;
}

Explanation with an example

public class Test {
int a, b;
}
public class MyClass{
Test t1 = new Test();
public int testMethod(int x){
int i = x + 3;
String str= "ABC";
//some stuff
}
}
  • t1 ( reference ) — Stack memory
  • Test object ( new Test() ) — Heap memory
  • int a, b — Heap memory ( inside the object. Because these are bound to the object )
  • testMethod — Stack Memory ( this is also a reference — for a method )
  • int x — Stack Memory ( this is a method parameter hence it is a local variable )
  • int i — Stack Memory ( local variable. This is not bound to the object )
  • String str — A string is created and it goes in the String Pool in the heap space and a reference is created in the testMethod() stack space for it.

Difference between Stack memory and Heap memory

Press enter or click to view image in full size

Based on the above explanations, we can easily conclude the following differences between Heap and Stack memory.

  1. Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
  2. Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.
  3. We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size.
  4. When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error.
  5. Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory is very fast when compared to heap memory.

How long does an object alive?

The Object life is dependent on references referring to it, if the references are dead then Object will die too.

What is a task for garbage collection (GC)?

This is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.

That’s all for this topic Memory Management in Java with examples.

Happy coding..

--

--

Written by Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com