Wednesday, September 3, 2014

JVM structure and working

Here I am posting some important points for java programmers who wants to explore about JVM.

Very first thing every java developer knows about JVM, JDK and JRE.
JVM (Java Virtual Machine) -- the JVM actually runs Java bytecode.
JDK  (Java Developer Kit) -- the JDK is what you need to compile Java source code
JRE  (Java Runtime Environment) -- is what you need to run a java program -- it contains a JVM, among other things.
i.e. JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries.
Now when a Java file is compiled it generates a class file which is a bytecode So this Java bytecode runs in a JRE.
What is Virtual machine? A virtual machine (VM) is a software implementation of a machine that executes programs like a physical machine. JVM analyzes and executes Java byte code.
Structure of JVM in diagram is: 

Lets look about Class loaders. Java provides a dynamic load feature. The Java Classloader is a part of the JRE that dynamically load java classes into the JVM. Class loader delegation flow 
The String Constant Pool is a special place where the collection of references to string objects are placed.
The Constant pool is a part of .class file (and its in-memory representation) that contains constants needed to run the code of that class.
Thread Pool Memory is place which consist of worker threads. It is to reduce overhead of memory management.
Class Method Area stores per-class structures like fields and method data, the code for methods. 
Heap is the place where objects are located at run time. 
Stack  involves role in method invocation and store procedure for execution of method per thread.
PC Register is Program Counter Register It contains the address of the Java virtual machine instruction currently being executed.
Native stack is the place which store information about native methods.

Now the Execution engine is the place where instructions are executed. There are different phase of execution

Sequential EE is the place where instructions are executed one by one sequentially.
JIT EE runs after the program has started and compiles the code on the fly into a form that's usually faster.
Adaptive optimize EE optimize the code before execution.

Native interface provide information and link for Native libraries which contains Native Function.