eBay interview question

what's the difference between stack and heap in memory

Interview Answer

Anonymous

20 Oct 2014

Main difference between stack and heap is that, stack is used to store local variables and function calls whereas heap is used to store the objects. Objects no matter where they are created are stored in the heap. Each thread has its own private stack where it stores its variables whereas each thread shares the heap. Heap memory is much larger than stack. Exceeding heap memory causes JVM to throw OutOfMemoryError. Exceeding stack memory causes JVM to throw StackOverFlowError. Recursive calls are generally stack intensive, they use up stack pretty soon.

8