Memory management in java
Heap: is a single area where JVM allocates memory for -Objects, including method code , static variables & instance variables.
Stack: Stack is created for each individual thread, JVM allocates memory for – local variables & arguments(reference) (values passed to method variables)
Note : interface – all values in interface are constants i.e final static, so it’s stored on Heap only.
When we have a declaration like this
class Sample{
int e = 1;
public int math (int x, int y){
A a = new A();
return (A.e + x + y);
}
}
Then we have:
Stack: x, y, a
Heap: instance a (it is A object), a.e = 1
(Note that a in stack points out to instance a in heap)
If instance a is no longer used, it is garbage collected
Reply