STACK MEMORY Explained
In my previous article, I have given the overview of JVM Memory . In this article we will try to understand how stack area of jvm memory works in details.
When a new stack is created:-
A new stack is created and pushed in the stack area of the jvm whenever a new thread is started.
The memory of a jvm stack does not need to be contiguos. Jvm performs only two operations on these stacks : Push and Pop.
For every method invokation which is made by a thread , jvm pushes a new STACK FRAME
in the stack designated to that thread.
Stack frame :- Before invokation of any method jvm evaluates how much memory will be utilized by that method and allocates that much memory to a new stack frame and then pushes this new stack frame to the stack area of the jvm.
For every method, a new stack frame is created which contains the local variables, parameters passed to the method,the intermediate computation results etc.
A stack frame is also called activation frame.
Structure of Stack Frame:-
Stack Frame consists of three parts:
1. Local variable array:- This is the part of the stack frame which contains local variables of that method. It also contains all the parameters passed to that method.
It keeps the data in an array.
2. Operand Stack :- This part of the stack memory is used to keep intermediate computation results.
How stack area is freed ???
After the completion of a method a stack frame becomes useless and similarly after a thread is completed the entire stack area designated to that thread at the time of creation is also useless.
So that stack is removed from the stack area by the jvm just before terminating the thread.
Note: In heap memory the cleaning up was taken care by garbage collection.
Thread Safety of Stack Area:-
Because each thread is designated a separate stack in the stack area , the data residing in stack area is always thread safe .
Thanks and feel free to comment in case of any doubt or suggestions about my articles.
When a new stack is created:-
A new stack is created and pushed in the stack area of the jvm whenever a new thread is started.
The memory of a jvm stack does not need to be contiguos. Jvm performs only two operations on these stacks : Push and Pop.
For every method invokation which is made by a thread , jvm pushes a new STACK FRAME
in the stack designated to that thread.
Stack frame :- Before invokation of any method jvm evaluates how much memory will be utilized by that method and allocates that much memory to a new stack frame and then pushes this new stack frame to the stack area of the jvm.
For every method, a new stack frame is created which contains the local variables, parameters passed to the method,the intermediate computation results etc.
A stack frame is also called activation frame.
Structure of Stack Frame:-
Stack Frame consists of three parts:
1. Local variable array:- This is the part of the stack frame which contains local variables of that method. It also contains all the parameters passed to that method.
It keeps the data in an array.
2. Operand Stack :- This part of the stack memory is used to keep intermediate computation results.
How stack area is freed ???
After the completion of a method a stack frame becomes useless and similarly after a thread is completed the entire stack area designated to that thread at the time of creation is also useless.
So that stack is removed from the stack area by the jvm just before terminating the thread.
Note: In heap memory the cleaning up was taken care by garbage collection.
Thread Safety of Stack Area:-
Because each thread is designated a separate stack in the stack area , the data residing in stack area is always thread safe .
Thanks and feel free to comment in case of any doubt or suggestions about my articles.
Comments
Post a Comment