Java virtual machine
is the heart of entire java program execution process. It is responsible for
taking the .class file and converting each byte code instruction into the
machine language instruction that can be executed by the microprocessor.
Memory management:
JVM architecture
JVM is mainly composed of two components - Classloader and
Execution Engine. The first component loads the .class files into memory. Then
it verifies whether the all byte code instructions are proper or not. If it
finds any instruction suspicious , the
execution is rejected immediately. If the byte instructions are proper , then
it allocates necessary memory o execute the program.
Execution engine contains a virtual processor, interpreter,
just –in-complier(JIT).
This memory is divided into 5 parts, called runtime data
areas, which contain the data and results while running the program.
Method area: The
Java Virtual Machine has a method area that is shared among all Java Virtual
Machine threads. It stores per-class structures such as the run-time constant
pool, code of variables and method data, and the code for methods and
constructors, including the special methods
used in class and instance initialization and interface initialization.
A Java Virtual Machine implementation may provide the programmer
or the user control over the initial size of the method area, as well as, in
the case of a varying-size method area, control over the maximum and minimum
method area size.
The following exceptional condition is associated with the
method area:
If memory in the
method area cannot be made available to satisfy an allocation request, the Java
Virtual Machine throws an OutOfMemoryError.
Heap: The heap is
the run-time data area from which memory for all class instances and arrays is
allocated. The heap is created on virtual machine start-up .The heap may be of
a fixed size or may be expanded as required by the computation and may be
contracted if a larger heap becomes unnecessary. Objects are created in this
area. Whenever jvm loads a class, a method and a heap area are immediately
created in it.
Java stacks: Each
Java Virtual Machine thread has a private Java Virtual Machine stack, created
at the same time as the thread. it holds
local variables and partial results, and plays a part in method invocation and
return. Java stacks are memory areas
where java methods executed. While
executing methods , a separate frame will be created in java stack, where the
method is executed. Jvm uses separate thread to execute each method.The memory
for a Java Virtual Machine stack does not need to be contiguous.
The following exceptional conditions are associated with
Java Virtual Machine stacks:
If the computation
in a thread requires a larger Java Virtual Machine stack than is permitted, the
Java Virtual Machine throws a StackOverflowError.
PC (program counter)
registers: The Java virtual machine can support many threads of execution
at once. Each Java virtual machine thread has its own pc (program counter)
register. At any point, each Java virtual machine thread is executing the code
of a single method, the current method for that thread. If that method is not
native, the pc register contains the address of the Java virtual machine instruction
currently being executed. If the method currently being executed by the thread
is native, the value of the Java virtual machine's pc register is undefined.
The Java virtual machine's pc register is wide enough to hold a return Address
or a native pointer on the specific platform.
Native method stacks:
java methods are executed on java stacks. Similarly , native methods are
executed on native method stacks. To execute the native methods, generally
native methods(for example c/c++ header files) are required. These header files
are located and connected to jvm by a program, called native method interface.
Example :
class Memory {
static int x;
/* static stack storage*/
public static
void main(String[] args){
Memory memory =
new Memory(); /* dynamic heap
storage*/
int y =0; /*
dynamic stack storage */
String myString = new
String("Memory"); /* dynamic heap storage */
}
}
When you create an object using the new operator, for
example, memory = new Memory();, it allocates memory for the memory object on
the heap. The stack memory space is used when you declare automatic variables.
when you do a string initialization, for example String
myString;, it is a reference to an object so it will be created using new and
hence it will be placed on the heap.
Memory space for objects is always allocated in heap.
Objects are placed on the heap. Built-in datatypes like int, double, float and
parameters to methods are allocated on the stack.
Execution engine contains interpreter and JIT(just in time)
compiler, which are responsible for converting the byte code instructions into
machine code so that the processor will execute them. Most of the jvm
implementations use both the interpreter and JIT compiler simultaneously to
convert the byte code. This technique is also called adaptive optimizer.
Garbage collection:
jvm also capable of deallocation the memory when it is not used. Suppose a
variable or an object is created in memory and is not used. Then after some
time it is automatically removed by garbage collector of jvm. Garbage collector
is a form of memory management that
checks the memory from time to time and marks the variables or objects not used
by the program automatically.
Some few important points about garbage collection in java:
1) Garbage collection is a mechanism provided by Java
Virtual Machine to reclaim heap space from objects which are eligible for
Garbage collection.
2) Garbage collection relieves java programmer from memory
management which is essential part of C++ programming and gives more time to
focus on business logic.
3) Garbage Collection in Java is carried by a daemon thread
called Garbage Collector.
4) Before removing an object from memory Garbage collection thread invokes
finalize () method of that object and gives an opportunity to perform any sort
of cleanup required.
5) a Java programmer cannot force Garbage collection in
Java; it will only trigger if JVM thinks it needs a garbage collection based on
Java heap size.
6) There are methods like System.gc () and Runtime.gc ()
which is used to send request of Garbage collection to JVM but it’s not
guaranteed that garbage collection will happen.
7) If there is no memory space for creating new object in
Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError
heap space
9) J2SE 5(Java 2 Standard Edition) adds a new feature called
Ergonomics goal of ergonomics is to provide good performance from the JVM with
minimum of command line tuning.
After reading this blog i very strong in this topics and this blog really helpful to all. Ruby on Rails Online Course India
ReplyDelete