Monday 22 February 2016

How Heap is different from Stack memory

Major difference between Heap and Stack memory are as follows-

  1. Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. 
  2. 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. 
                       //Heap Memory allocation
                          A obj=new A();
                        //Stack Memory alllocation
                          int a;A obj;

     3. Memory management in stack is done in LIFO manner whereas it’s more complex in Heap memory because it’s used globally. 

      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 an error.

      5. 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.

To Understand Memory Managemet follow this link..
 
https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html#wp1085825

No comments:

Post a Comment