JVM architechture

Architecture of the JVM (java virtual machine)


 JVM is made up of following modules:-
              1:- class loader
              2:- byte code verifier
              3:- memory area
              4:- execution engine

1:- class loader
the class loader loads the byte code(.class file) from the hard disk to the RAM and it is accomplish its task by three ways that are following:-
             1:- Bootstrap loader
             2:- Extended class loader
             3:- System class loader/class path
                                1:- Bootstrap loader
*I am use linux mint so the below path is for linux mint only.
 the bootstrap loader loads the file from,                            
   /usr/lib/jvm/java-ibm-x86_64-80 /jre/lib/rt.jar
(for execution of the java program the jvm use the public jre if it is exiest. if is not present the jvm use the private jre which is in the JDK package.)
the path of the private jre is:-
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar

any class we use in our java program is firstly searched by class loader in the bootstrap loader i.e. in the above two rt.jar files.

for the windows user the above rt.jar file location is at c:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar
if you wish to put your .class file into this rt.jar then simply follow the following steps:-
 1:-create a java file with public class and package declaration.
 2:-complie the java file with javac -d <destination for the compiled file> <source java file name>
example:- javac -d . Student.java
 3:-for putting your .class file into rt.jar you have to first extract the rt.jar by following command,
  jar -xvf rt.jar .
(. is a oprator which is used to refer the present current directory for the destination)
 after extracting put your .class file along with the extracted folder (first copy the rt.jar in the seperate folder and then do all these operation for the safty of default class files of the rt.jar)
 :- now use the following command,
 jar -cvf rt.jar 

                             2:- Extended class loader
If the JVM does not found the .class file in the bootstrap loader i.e. in the rt.jar then it will find it into the ext folder located in the "lib" folder in the above specified path both in linux and in windows.
in the ext folder you can stored your .jar file and all .class files must have the package declaration in them. 
NOTE:- if you are working in any project and want to add some additional .class files of any third party vendors for java framework or anything then you can copy the jar file into this folder so that you can use its functionality in your application.

the public jre path for the extended class loader in linux is,
/usr/lib/jvm/java-ibm-x86_64-80 /jre/lib/ext

and the private jre path for the extended class loader in linux is,
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext

extended class loader location in windows is,
c:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

                        3:- System class loader/class path
when the jvm does not find the .class file in the rt.jar and in the ext folder then it will find it in the default package i.e. System class loader/class path. the drive in which you installed the Operating System and jdk (java development kit) that drive becomes the default package for the jvm and at the last jvm will search the .class file in the default package. you can set the class path in windows if you have multiple partition of the drive.
 you can set up class path by, System properties/advance setting/environment variables/
 then set the name of class path variable 
and value ="c:\Program Files\Java\jdk1.8.0_25\bin


2:-byte code verifier
after loading the byte code to the RAM the Java virtual machine verify the byte code by its next module byte code verifier. 
This process is important because of the byte code(.class file) is stored in the computer at OS level that's why any user who use the computer can intentionally or accidentally change or modify the content of the .class file which can cause, abnormal termination of the application and unexpected results. so the byte code verifier 
checks that their is no explicitly changes made in .class file.


3:- memory area
after the successful verification of the byte code the byte code is now split in distinct modules and stored in memory areas.
the various memory area are as follows:-
           1:-class area
           2:-stack area
           3:-heap area
           4:-native area
           5:-PC register
                              1:-class area
In any java program we use multiple classes.
for example:- To write a simple "hello" program we use four classes and that classes are :-
            1:- lang.System class
            2:- io.PrintStream class
            3:- lang.String class
            4:- user define class for public static void main(String[] args)
so these four classes will be loaded in the RAM at class area of the JVM and these four classes will get the separate class area i.e. class area 1, class area 2, class area 3, class area 4.

                            



                                     
                                  2:-stack area
In stack area methods will be stored according to their calling that means in the stack the calling method will get memory in stack over the called method.
for example:- if main() will call m1() and m1() will call the m2() then the sequence of methods in the stack will be as follows,

          
                                    3:-Heap area
The heap area of the memory is used to store the objects created in the whole program.

                                     4:-Native area
The native area contains the native language code. The native languages are C/C++.
                                      5:-PC register
The PC is stands for Program Counter. PC register is contains the address of the next instuction to be executed in the program.

4:Execution Engine
the execution engine is executes the program by interacting to memory area and Operating System.

Comments

Popular posts from this blog

Inter-Thread communication

define System.out.println()?

Reflection in java