Difference between stack memory and heap memory in java

javamemory

                 Stack              Heap

1)      Used to store local variables,address of the objects and method calls.

1)      Used to store Object values in java.
2)      When there is shortage of memory in stack ,JVM throws java.lang.StackOverflowError. 2)When there is shortage of memory in heap JVM throws java.lang.OutOfMemoryError.
3)      Data in the stack dies or gets deleted once the block of code or method call terminates. 3)Data in the heap dies till the object gets garbage collected.
4)      Stack is faster when compared to heap 4)Heap is slower when compared to stack
5)      Variables created in stack are visible only to the thread which it has created. 5)Objects in heap are visible to all threads.

Thanks to interviewdot.com for the video.click here to submit your resume in interviewdot.com

2 thoughts on “Difference between stack memory and heap memory in java

Leave a Reply

Your email address will not be published. Required fields are marked *