Sr. No. | Key | Stack | Heap Memory |
---|---|---|---|
1 | Basic | Stack memory is used to store items which have a very short life like local variables, a reference variable of objects | Heap memory is allocated to store objects and JRE classes. |
2 | Ordering | The stack is always reserved in a LIFO (last in first out) order | Heap memory is dynamic allocation there is no fixed pattern for allocating and deallocating blocks in memory |
3 | Size | We can increase stack memory size by using JVM parameter -XSS | We can increase or decrease heap memory size by using JVM option -Xms and -Xmx |
4 | Visibility | Variables are visible to only to owner thread | It is visible to all threads |
5 | Exception | JVM will throw java.lang.StackOverFlowError | JVM will throw java.lang.OutOfMemoryError |