JVM (Java Virtual Machine)
JVM (Java Virtual Machine)
After loading the .class file into the method area immediately
jvm creates an object for that loaded class in the heap
memory.......of TYPE (java.lang.class)
The object created is not of the type of .class file
Instead it is of type “CLASS”....here total 3 parts of memory are
involved ie 2parts of JVM and 1 part of our
Local hard disk.
WHY DOES JVM CREATE THE OBJECT OF THAT LOADED CLASS
IN HEAP AREA????
ANS: THE OBJECT of class “CLASS”representing the actual .class
file provides complete information of the .class to
programmers......ie HOW MANY METHODS ARE PRESENT
…,constructor info.....how many constants present...and so
on....!!
The class “CLASS” object can be used by programmer to get
class information.....
Eg:
Class student same as above
Class test{
P s v main(Strings args[]){
Student s1=new studenr();
Class c1=s1.getClass();
Student s2=new student();
Class x2=s2.getClass(); System.out.println(c1.hashCode);
System.out.println(x2.hashCode);
System.out.println(c1==x2);
Output
123345454444
123345454444
TRUE
VERFICATION:
It is the process of ensuring that binary representation of class
is structurally correct or not..! Ie JVM will check whether the
.class file is generated by valid complier or not ie whether .class
file is properly formatted or not.
PREPARATION :
In this phase jvm will allocate memory for class level static
variables and assign default values.
In initialization phase the original values will be assigned to the
static variables.....but here only default values will be allocated
RESOLUTION:
It is the process of replacing symbolic names in our program
with original memory refrences from method area .
Eg:
Class test{
Public static void main (String args[]){
String s=new String(“DURGA”);
Student ss1 =new Student();
}
}
INITAILIZATION:
EXTENSION CLASSLOADER:
Extension class loader is the child class of
bootstrap class loader .
It is responsible to load class from extension
class path.....ie
Jdk/jre/lib/ext/*.jar(any class present here is
loaded)
• Extension class loader is implemented in java
and corresponding .class file is
sun.misc.Launcher$ExtClassLoader.class
APPLICATION CLASS LAODER OR SYSTEM CLASS
LOADER:
• IT is the child class of Extension class loader.
• This class loader is responsible to load classes
from application class path’
• It internally uses environmental class path.
• Application class loader is Implemented in java
and corresponding .class name is
Sun.misc.Launcher$AppClassLoader.class
HOW CLASS LOADER WORKS??
@Override
public Class<?> loadClass(String name) throws
ClassNotFoundException {
//check for updates and return coreesponding class object
//if specified .class file is not available throw class not found
exception
return super.loadClass(name);
}
HEAP AREA:
• For every JVM one heap area is available
• Heap area will be created at the time of JVM startup
• Objects and correponding instance variables will be stored in
the heap area
• Every array in java is object only hence arrays will also be
stored in heap area
• Heap area can be accesed by multiple threads hence data
stord in the heap memory is not thread safe.
• HEAP area need not be continuous.
Runtime r=Runtime.getRuntime();
Once we get runtime object we can call the following methods
on that object
1. maxMemory();(returns number of bytes of max memory
allocated to the heap)
2. totalMemory(); it returns number of bytes of total memory
allocated to the heap (initial memory)
3. freeMemory();it returns number of bytes of free memoory
present in the heap.
Eg:
}
Output:
max memory 1801 mb
initial memory 123 mb
free memory 121 mb
consumed memory 1 mb
STACK MEMORY:
For every thread JVm will create a separate stack at the time of
thread Creation
Each and every method call performed by that thread will be
stored in the stack including local variables...
After completing a method the corresponding entry from the
stack will be removed after completing all method calls the
stack will become empty …..and just before the termination of
thread
That empty stack will be destroyed by the JVM.
Each entry in the stack is called STACK FRAME
OPERAND STACK:
JVM uses operand stack as workspace some instructions can
push the values to the operand stack
And some instructions can perform required operations
Basic idea of operand stack.....
instructions as mentioned earlier such as iload-0,iload-1,iadd,I
store these are assembly imstructions we don’t have to worry
about........
FRAME DATA:
CONCLUSIONS:
Method area ,Heap area, STACK area are considered as
important memory areas with respect to programmer
Method area and heap area are per jvm
Stack area ,pc registers ,native method stack …..are per thread.
Static variables are stored in Method area.
Instance variables in Heap area
Local variables in Stack area.
Class test{
Student s1=new Student();
Static Student s2=new Student();
P s v main(String args[]){
Test t=new Test();
Student s3=new Student();
}
}
INTREPRETOR:
It is responsible to read byte code and intrepret into machine
code (native code) and execute that machine code line by line.
The problem with interpretor is it interprets everytime
Even same method invoked multiple times......which reduces
performance of the system.
To overcome this problem sun people introduced JIT compiler in
1.1 version.
JITM COMPILER:
The main purpose of jit compiler is to improve performance
internally …....jit compiler maintains a separate count for every
method
Whenever JVM come across any method call the methods gets
interpreted normally …..by the interpretor and jit compiler
increments...the corresponding count variable...if the count
reaches then threshold count...
Then jit compiler indentifies that the method is a repeatedly used
method ….such methods are called “HOT SPOTS”immediately jit
compiler compiles that method and generates the corresponding
native code...
Next time if the JVM comes across that method call then the JVM
uses the native code directly and executes it.....instead of again
interpreting it once again..so that performance of system is
improved....
NOTE:
JVM INTERPRETS the total program atleast once …..
JIT compilation is applicable only for repeatedly required
methods not for every method...
magic_number;
minor_version;
major_version;
constant_pool_count;
constant_pool[];
access_flags;
this_class;
super_class;
interface_count;
interface[];
fields_count;
fields[];
methods_count;
methods[];
attributes_count;
attributes[];
}
magic_number;:
the first 4 bytes in every .class file should be....0XCAFEBABE
So that the JVM VERIFIES WHETHER .class FILE IS GENERATED BY
A VALID COMPILER.
This value is predefined …...
When ever we are executing a java class if the JVM is not able to
find valid magic number we get runtime exception saying.
Java.lang.ClassformatError:Incompatible magic value
Major and minor version represent .class file version
Jvm will use these versions to identify which version of compiler
the current .class file
M.m(M->major m->minor)
1.5v-m->value 49.0
LOwer version copmiler generated .class files can be run by
higher version JVM.
Fields_count:
Number of fields present in current class.
Here fields are Static variable,static bloacks////etc
Methods_count:
It represents number of methods present in current class.
Methods_array:provides information about all methods present
in current class
Attributes_count:
It returns/represents number of attributes present in current
class.
Attributes_count:
It provides information attributes present in current class.
Attributes are instance variables.