Wednesday, January 2, 2013

JVM - the runtime data areas (Part 1/2)

Hi, This is Part 1 of a 2 part series that unfolds the abstract view of runtime memory allocation. You will understand how the variables get memory, how methods are created, how the control might flow. I have explained it in layman's language, for insight I will add links to a few external resources. Link to part 2 is provided at the bottom of this article.

I have created a few diagrams that inform you about important memory blocks present inside a JVM instance when you hit  java HelloWorld, and how your variables are loaded, where objects get memory, how method stacks are created and destroyed.


JVM instance data areas
Info: Click on the thumbnail to view in original size.

1) Method Area
When a class is loaded, then all the class level data like the static fields, methods, etc. are loaded here, whose value can be known at compile time. The fields get their default values first, and then initialization values, if provided. For example, if its like:
- static int i = 10;
"i" will store 0 first as its default value, but once JVM does its interpretation, it will replace 0 with 10.
- static SomeClass someRef;
"someRef" will store null.
- static SomeClass someRef = new SomeClass();
"someRef" will store null first. Later an object will be created in heap area, following which null will be overwritten by reference to that object's location.
- int j = 20;
"j" is an instance variable, so its not loaded as during compile time, there's no way to know its state.
Even the public static void main(String...args){...} method is loaded here, but please mind, not executed.
The method area is a shared memory location.

2) Heap Area
Heap area is meant to store only objects and nothing else. It is a shared memory location.

3) Stacks Area
By default, only main thread is there where we run our code. Each thread has its own stacks area. All the methods are executed here in individual stack frame. The local variables also get memory inside corresponding method's stack frame. Local variable are never initialized automatically, so you always see error like "This variable might not have been initialized!" during compilation itself.
When a method is called, its stack frame is created, and logic executed inside that. If you call another method, then another stack frame is created for that called method and its logic is executed. When execution of a method is completely over, the stack frame is destroyed, memory freed, and control returns to the line of invocation.
When there's not enough memory for creating more stacks, you get java.lang.StackOverflowError.

4) Program Counter Register
The PC register stores the address of the instructions currently being executed by the JVM. Each JVM thread has its own pc register. For native methods, the pc register is not used at all.

5) Native Methods Area
This area supports native method stacks, which are written in language other than Java, and used by the JVM.

Our focus is only the first 3 at this moment as we unfolfd how a simple program is allocated runtime memory for its components like variables and methods. Please proceed to part 2 of this 2 part series.

No comments:

Post a Comment

Liked or hated the post? Leave your words of wisdom! Thank you :)