Monday, 25 April 2016

Heap vs. Stack memory


Heap vs. Stack memory
    
CONTENTS
  • Objects are always stored in Heap 
  • Reference variables to objects and primitive variables are stored in Stack memory

USABILITY
  • Heap memory is used globally by all components of the application
  • Stack memory is used only by one thread of execution.

LIFE
  • Heap memory lives from the start till the end of application execution.
  • Stack memory is short-lived.

SIZE
  • Stack memory size is very less when compared to Heap memory.

ERRORS   
  • When stack memory is full, JRE throws java.lang.StackOverFlowError
  • If heap memory is full, it throws java.lang.OutOfMemoryError

SPEED
  • Stack memory is very fast when compared to heap memory due to simplicity in memory allocation (LIFO).

JVM OPTIONS    
  • Use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory.
  • Use -Xss to define the stack memory size.

MEMORY MANAGEMENT
  • Heap memory is divided into Young-Generation, Old-Generation etc.
  • Memory management in stack is done in LIFO manner

No comments:

Post a Comment

Note: only a member of this blog may post a comment.