Java Interview Question

Use this intermediate question and answer to prepare for your next Java interview.

Explain the difference between Heap and Stack in Java.


The Heap Memory in Java is the space used by the JVM Runtime to allocate memory and store objects used throughout the program. Heap objects are shared across the entire program. The Heap has a special space called PermGen which is used for holding metadata about user classes and some other advanced functions.

The Stack Memory is used by the JVM for storing data local to Threads and Methods. It stores primitive variables and references to heap objects that are being used by the current method or thread. The stack operates in LIFO (Last-In-First-Out) order.

The garbage collector goes through objects in the Heap and removes those which don’t have any references in the program. The Stack is not garbage collected and it is much smaller and more transient compared to the Heap.

Looking for developers?

Want to learn more about Gigster? Enter your email and we'll be in touch.