C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

What are the differences between Heap and Stack Memory in Java?

Here, we are going to learn heap and stack memory in java? What are the differences between heap and stack memory in java? Compare and contrast heap and stack memory.
Submitted by Preeti Jain, on February 03, 2018

1) Stack Memory in Java

  • For stack memory, JVM creates a separate runtime stack for every individual thread at the time thread instantiation (creation).
  • No "new" keyword is required for stack memory.
  • Every method call performed by that thread and method will be stored in corresponding runtime stack (i.e new created thread is responsible to call its method and the method entry will be stored in desired run time stack at the time of that thread instantiation).

Example:

class ChildThread extends Thread{

	public void run(){
		System.out.println("I am in child thread");
	}

}

class MainThread{
	public static void main(String[] args){
		ChildThread ct = new ChildThread();
		ct.start(); 

		public static void m1(){
			System.out.println("I am in main thread");
		}
	}
}

In the above example, we have two threads:

  1. Main thread
  2. Child thread
Runtime stack for Child Runtime stack for Main
run() m1() method
main() method

  • After executing method the corresponding method entry from the stack will be removed.
  • After executing all method calls the stack will become empty and that empty stack will be destroyed by the JVM before terminating the thread.
  • Stack memory is not global (i.e. one stack variable or methods can't be used in another stack).
  • Each entry in the stack is called stack frame or activation records.
  • JVM creates a separate runtime stack for every thread (i.e data stored in the stack is available to the corresponding thread only and another thread can't access that data it means data is private to that thread and that’s why we call it is thread safe).

Stack frame are made up of three parts:

  1. Local variable array:
    • It contains all arguments (or parameters) and local variables of the methods.
    • Each block in array is of 4 bytes Values of int, float and reference occupy only one block and double and long occupy two block and byte, short and char will be converted into int before storing.
  2. Operand stack:
    • It is a stack to perform intermediate data or results.
    • Frame Data: It contains constants and reference to exception table which provide corresponding catch block information in the case of exception.

2) Heap Memory in Java

  • Heap memory will be created at the time of JVM startup.
  • "new" keyword is required for heap memory.
  • Heap memory contains Object and Object contains instance data (i.e heap contains Object and instance data).
  • The data stored in heap memory is not thread safe (i.e. Object of one thread will be shared by another thread and data is not private).
  • Only one heap memory is allowed (not like stack memory) and that memory will be shared by multiple threads.
  • We can create Runtime object and once we got Runtime object then we call Runtime specific methods like,

Methods: maxMemory(), totalMemory(), freeMemory()

Sample:

    Runtime r = Runtime.getRuntime();

    r.maxMemory();
    r.totalMemory();
    r.freeMemory();

Explanation:

  1. maxMemory(): It returns the number of bytes of max memory allocated to the heap.
  2. totalMemory(): It returns the number of bytes of total memory allocated to the heap.
  3. freeMemory(): It returns the number of bytes of free memory present in the heap.

Example:

class Heap{

	public static void main(String[] args){
		Runtime r = Runtime.getRuntime();
		
		long max_memory = r.maxMemory();
		long total_memory = r.totalMemory();
		long free_memory = r.freeMemory();
		
		System.out.println(" Maximum Memory in bytes " + max_memory);
		System.out.println(" Total Memory in bytes " + total_memory);
		System.out.println(" Free Memory in bytes " + free_memory);
	} 

}

Output:

D:\Java Articles>java Heap
Maximum Memory in bytes 1888485376
Total Memory in bytes 128974848
Free Memory in bytes 127611672


Liked this article? Do share with your friends :)


COMMENTS