Saturday, March 2, 2013

Internal work of JVM




In our previuos section we have learned first java program 'helloworld", how to compile and hoe to run,now we are going to learn how the program is compliling and running and also what is the JVM ,and its internal flow,moreover some question on this first java program.
what happens at Compile time?
After compiled the "helloworld.java" by java compiler using javac.it will convert the java code into byte code.
% javac HelloWorld.java

After the execution of this command,the .class file i.e:HelloWorld.class is created.
In other words,it is the dos commad given to the java compiler to convert contents of the mentioned source into its equilant byte code.

What happens at  Runtime?
Now here JVM role comes,here classloader will loads the "System.lang.classloader"  finds the Helloworld class by searching the class path given.
Interpreter will read the byte code stream and then transfer to JVM.
JVM will extracts the all the information from the bytecode stream,as follows
Constants like literals,symbolic refernces to types,fields,methods will be stored in constants pool.
methods information like modifers,type ,static variables,package,return type,method parameters will stored in "Method area".
Once extracts the information ,JVM will find for public static void main(String args[]).
JVM memory consists following segments.
Heap Memory, which is the storage for Java objects
Non-Heap Memory, which is used by Java to store loaded classes and other meta-data
JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.
In this program public,static,void are keywords.
main() is a method.
String is a class.
println is a method and system is a class.

constants( like literals, constants, symbolic references to types, fields, and methods) will be stored in constant pool [in this case, symbolic reference to HelloWorld class and the fields, methods, constants associated with it]
package,modifiers,static variables [in this case "HELLOWORLD" attribute],
field information(name,type,modifiers) -
methods information(name,return type,method parameters,modifiers, method’s bytecode) – in this case , its [print, void, public and byte code]
reference to ClassLoader [ which classloader loads this class ]
reference to class Class
and store the above information in “Method Area“.
After loading that information, i as jvm(thread) will try to find out “public static void main(String [] args)” method.
Now the following command is given to the JVM to execute the .class file.
% java HelloWorld

In the process of executing the .class file one of the responsibility of the JVM is to convert the .class file into equilant executable code.

No comments:

Post a Comment