Java Stack and Heap: Java Memory Allocation Lesson.
Stack in java is a section of memory which contains methods, local variables, and reference variables. Local variables are created in the stack.
Java Stack memory is used for the execution of a thread. They contain method-specific values that are short-lived and references to other objects in the heap that is getting referred from the method.
Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method.
As soon as the method ends, the block becomes unused and becomes available for the next method.
Stack memory size is very less compared to Heap memory.
Key Features of Stack Memory
Apart from what we have discussed so far, following are some other features of Stack memory:
- It grows and shrinks as new methods are called and returned respectively
- Variables inside stack exist only as long as the method that created them is running
- It’s automatically allocated and deallocated when method finishes execution
- If this memory is full, Java throws java.lang.StackOverFlowError
- Access to this memory is fast when compared to heap memory
- This memory is thread-safe as each thread operates in its own stack
Methods in Stack class
- Object push(Object element): Pushes an element on the top of the stack.
- Object pop(): Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.
- Object peek(): Returns the element on the top of the stack, but does not remove it.
- Boolean empty(): It returns true if nothing is on the top of the stack. Else, returns false.
- int search(Object element): It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.
Heap Memory
Heap is a section of memory which contains Objects and may also contain reference variables. Instance variables are created in the heap.
The memory is allocated during the execution of instructions written by programmers. Note that the name heap has nothing to do with heap data structure. It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. If a programmer does not handle this memory well, a memory leak can happen in the program.
Key Features of Java Heap Memory
- Apart from what we have discussed so far, following are some other features of heap space:
- It’s accessed via complex memory management techniques that include Young Generation, Old or Tenured Generation, and Permanent Generation
- If heap space is full, Java throws java.lang.OutOfMemoryError
- Access to this memory is relatively slower than stack memory
- This memory, in contrast to stack, isn’t automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage
- Unlike stack, a heap isn’t thread-safe and needs to be guarded by properly synchronizing the code
Difference Between Stack and Heap Memory
- 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.
- Objects stored in the heap are globally accessible whereas stack memory can’t be accessed by other threads.
- Memory management in the stack is done in a LIFO manner whereas it’s more complex in Heap memory because it’s used globally. Heap memory is divided into Young-Generation, Old-Generation etc, more details at Java Garbage Collection.
- Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.
- We can use -XMX and -XMS JVM option to define the startup size and maximum size of heap memory. We can use -XSS to define the stack memory size.
- 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.
- 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.
PARAMETER |
STACK |
HEAP |
Basic |
Memory is allocated in a Contiguous Block |
Memory is allocated in a Random Order |
Allocation and Deallocation |
Automatic by compiler |
Manual by Programmer |
Cost |
Less |
More |
Implementation |
Hard |
Easy |
Access time |
Faster |
Slower |
Main Issue |
Shortage of Memory |
Memory Fragmentation |
Locality of Difference |
Excellent |
Adequate |
Flexibility |
Fixed-Rate |
Resizing is possible |
Comments
Post a Comment