Heap and Stack in Java
We are discussing few interview questions from our earlier tutorials and its a follow-up with same interview questions in Java. Questions are
How memory managed in java?
What is Heap and Stack memory?
What will be stored in Heap and Stack?
Determine the size of Heap and Stack?
Stack vs Heap Pros and Cons?
When to use Heap and Stack?
This is one of the important questions which asked in most of the Java interviews and later interviewer may jump into asking question in Operating System(OS) level memory management also. Lets see above questions one by one and finally its a simple programs where we all can discuss about the memory allocated for it in Java.
How memory managed in Java?
Stack and Heap are the memories allocated by OS to JVM and they both are stored in the computer's RAM (Random Access Memory).
What is Heap and Stack memory?
Stack:
It's a special region of computer's memory that stores temporary variables created by each functions. Stack uses "FILO" (First In Last Out) data structure, where its managed and optimized by CPU closely.
Heap:
Heap is a region of computer's memory that is not managed automatically by programmer, and is not as tightly managed by the CPU. It is a more free-floating region of memory.
What will be stored in Heap and Stack memory?
Stack:
Methods and the local variables are stored in stack. Also it is to be remembered that the variable references primitive or object references will be stored in Stack.
Heap:
Objects and its instance variable are stored in Heap. In Java 6 due to "escape analysis" optimization, sometimes objects will be stores in Stack also.
Determine the size of Heap and Stack?
Stack:
Stack is usually pre-allocated and limited in size, because by definition it must be contiguous memory. Also depends on the language, compiler, operating system and architecture.
Heap:
In general Heap will be dynamically allocated and constantly changing in size.
Stack vs Heap Pros and Cons?
Stack:
Very fast in access.
Explicitly can't de-allocate variables allocated in Stack.
Memory fragmentation will not happen in Stack, since its managed efficiently by CPU.
Stack size is limited depending on OS.
If Stack runs out of memory, then this leeds to stack overflow could cause program to crash.
Heap:
Slower in access compared to Stack.
No limited on memory size which will be dynamically allocated.
Heap could cause fragmentation problem, which occurs when memory on the heap is being stored as non-contiguous blocks.
When to use Heap and Stack?
Its better to choice Stack when we use less memory or small amount of data in usage. Suppose if we don't know about the size or application uses large amount of memory like dynamic array or collections then we need to go for Heap.
Lets discuss:
public class MyProgram {
private static int value = 10;
private String str = new String("Java Discover");
private String name = "Heap and Stack";
public void sayHello(){
System.out.println("Hello, Java Discover !!!");
}
public static void main(String[] args) {
String country = "India";
MyProgram obj1;
MyProgram obj2 = new MyProgram();
obj2.sayHello();
}
}
Lets comment on the memory usage on above program as what will be stored in Stack and Heap?