Difference between Heap and Stack Memory in Java

When you are going to the java interviews this is one of the frequently asked questions for Java developer, fresher, or experienced.

What Is Java Stack?

A Java stack is part of your computer’s memory where temporary variables, which are created when you create a method, are stored. It is used to execute a thread and may have certain short-lived values as well as references to other objects.

The memory size of a Java stack is generally much less than in a Java heap space because when a method ends, all the variables created on the stack are erased forever.

What Is Java Heap?

Java objects created in the area is called the heap. It is created when the program is run, and its size may decrease or increase as your program runs. It can easily become full. When the heap area becomes full then garbage collection is initiated. This is when objects that are no longer used are deleted to make way for new objects.

Heap and Stack both are part of JVM and both consumer's memory allocated to the Java process, there are many differences between them e.g. Heap memory is shared by all threads of Java application but Stack memory is local to each thread. Objects are created in heap memory but method frames are stored in Stack memory, and the size of heap space is much bigger than the small size of Stack in Java.

Both of these are used for a different purpose,

1). The main difference between heap and stack is that stack memory is used to store local variables and function calls while heap memory is used to store objects in Java. It doesn't matter where the object is created in code e.g. as a member variable, local variable, or class variable, they are always created inside heap space in Java.

2). JavaVirtual Machine allows you to resize both heap and stack in Java, but for this, you need to use different JVM flags for that. You can use -Xms and -Xmx to specify the starting and maximum heap memory in Java. In the same manner, you can use the -Xss to specify the stack size of individual threads in JVM.

3). Heap memory is shared by all threads hence it is also known as the main memory but stack memory is local to threads and local variable created there was not visible to others.

4). Heap is a large memory area where objects can be created and stored in any order, but Stack memory is structured as Stack data structure i.e. LIFO where method calls are stored as last in first out order. These features enable us to do recursion in Java.

5). Objects created in the heap are visible to all threads. Variables stored in stacks are only visible to the owner Thread, It means Stack memory is kind of private memory of Java Threads, Heap memory is shared among all the threads.

6). Heap space in Java is much bigger than the Stack memory. Whenever a new thread is created in JVM, separate stack memory is allocated to them.

7). One little exception of that is String literals which live in String pool, which was not part of the heap until Java 7. Earlier String pool was created on metaspace, which was separate memory are in JVM used to store class metadata, but from JDK 7 onwards String pool is merged into heap space. Stack memory is used to store local variables e.g. primitive int and boolean variables, method frames, and call stack.

8). If there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space. If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError.Eg.When we create or use recursion in our programming, If We forgot to write to break the recussion then we get this error.

In short

Stack

  • The size of the stack will vary as methods and functions to create and delete local variables as needed.
  • Memory is allocated and then subsequently freed without you needing to manage the memory allocation.
  • Stack has size limits, which can vary according to the operating system you use.
  • Variables that are stored on the stack exist for as long as the function that created them is running.

Heap

  • Memory is not managed automatically nor is it as tightly managed by the central processing unit the way stack is managed.
  • You would need to free allocated memory yourself when these blocks are no longer needed.
  • The heap is prone to memory leaks, where memory is allocated to unused objects and will not be available to processes other than that.
  • There is no size limit in the heap.
  • Compared to stack, objects in the heap are much slower to access. It is also slower to write to the memory on the heap.

That's all on the difference between Stack and Heap memory in Java.