The JVM runtime data areas are used
in program execution. Some are created on JVM start-up and destroyed when JVM
exits, while others are per thread which created when thread is created and
destroyed when thread exits. Following are the various runtime data areas :
1. Method Area: Method area is created on JVM start-up and shared
between all the JVM threads. Complied java file or .class file is dumped in to
method area. Method area stores class structure in following parts:
- Type Information – fully qualified type’s name, super class name, type’s modifiers, whether class or interface
- Run-time constant pool – an ordered set of constants like string, integer, final variables etc. It also stores symbolic references to types, methods and fields
- Field Information – field name, type and modifiers. A Java field is a variable inside a class.
- Method Information – Method’s name, return type, modifiers. The number and types (in order) of the method's parameters.
- Class variables – ordered set of class variables (static variables).
- Reference to Class loader and class Class - reference to class loader is used for dynamic linking. instance java.lang.Class is created every type for the following info.
o getName();
o getSuperClass();
o isInterface();
o getInterfaces();
o getClassLoader();
·
Method
table - For each non-abstract class
a Java virtual machine loads, it could generate a method table and include it
as part of the class information it stores in the method area. A method table
is an array of direct references to all the instance methods that may be
invoked on a class instance, including instance methods inherited from
superclasses.
2. Heap Area: Objects and
arrays are allocated in this area. There
is only one heap inside a Java virtual machine instance and all threads share
it. The heap is created
on virtual machine start-up. The heap size can be changed using commands :
-Xms - Set initial Java heap size
-Xmx - Set maximum Java heap size
Insufficient Heap size will cause java.lang.OutOfMemoryError:
Java heap space. To free memory from heap java uses garbage collector.
3. Stack Area: Java virtual machine creates a new Java stack for
each thread. Each and every method called by thread will be stored in
corresponding Stack including local variables. When a thread invokes a Java
method, the virtual machine creates and pushes a new frame onto the thread's
Java stack. Each entry in stack called stack frame which contains local variable array, Operand stack,
frame data and reference to runtime constant pool for class of the current
method. All the data on a thread's Java stack is private to that thread. There
is no way for a thread to access or alter the Java stack of another thread.
Because of this, local variables are
thread safe in multi threading environment.
Stack Frame - used
to store data and partial results, as well as to perform dynamic linking,
return values for methods, and dispatch exceptions. A new frame is created each
time a method is invoked. A frame is destroyed when its method invocation
completes (Normal or abrupt/exception). Stack frame has three parts: local
variables, operand stack, and frame data
·
Local Variables –
o The sizes of the local variables are measured in
words and determined at compile time, which further added in the class file
data for each method.
o The local variables section of the Java stack frame
is organized as a zero-based array of words.
o Values of type long and double occupy
two consecutive cells in the array, rest takes one cell.
·
Operand Stack –
o
This
area is used by all the opcode instructions (portion of a machine language instruction that
specifies the operation to be performed).
o Size of the operand stack is measured in words and
determined at compile time, which further added in the class file data for each
method.
o
Like the local variables, the operand stack
is organized as an array of words but accessed by push and pop values.
o
JVM uses the operand stack as a work space.
Many instructions pop values from the operand stack, operate on them, and push
the result.
·
Frame
Data –
o
Includes data to support constant pool
resolution, normal method return, and exception dispatch.
o
JVM uses the frame data's pointer to the
constant pool to access the information when it encounters an instruction that
refer to an entry in the constant pool
o
Frame data also help JVM in processing a
normal or abrupt (exception) method completion. In case of normal method
completion JVM restore the stack frame of the invoking method, where as in case
of exception JVM uses the exception table referred to by the frame data to
determine how to handle the exception.
4. PC Register - Holds
address of current executing instruction. If the current
executing method is ‘native’, then the value of program counter register
will be undefined. The pc register is one word in size, so it can hold both a
native pointer and a returnAddress. For every thread a separate PC
registers will be created.
5. Native
method Stacks – Native method
stacks are typically allocated per thread when each thread is created. Native
methods (methods written in a language other than the Java programming
language) are implementation dependent. The designers of native method decide a
mechanism that how java application will invoke native methods. When a thread
invokes a native method JVM does not create a new stack frame for that as in
case of java method invocation, but it will simply dynamically link to and
directly invoke the native method.
Related Topics :