HTML Java

Java JVM Architecture


JVM Architecture

The main function of JVM is To load and execute your application its generally a .class file when you get .class file

When you javac Hello.java then you get Hello.class file and this file you have to execute so I have to called java Hello when you called your class through java command you are calling JVM instance now look How JVM instance loads and executes you class file there is 2 components Class Loader and Execution Engine so the class loader is responsible for loading your Hello.class File and also load built in class file for Example String.class and Object.class and all the collection classes etc. then the loaded class which are basically bytecode instruction are fed into the Execution Engine and this is responsible for executing byte code so in order to execute the bytecode it needs to talk to the OS in order to execute the instruction finally against the machine instruction set so for that execution engines make use of native method calls and finally it translated into machine codes value being responsible for carrying out the operations .



Class Loader

Class loading is the process in which bytecode and the data will be loaded upto a memory area so in actual JVM architecture there is one more component involved called data areas or memory areas .



Class Loader Subsystem How this work?

Class loading has 3 main phases load link and initialize so load is responsible to load the bytecode into the memory so loading can load .class bytecode from various sources if we call java Hello Hello Will be in the .class file so the class loader would read it from the file system class loader may read from the .jar file which contains a .class file and it can read from the various sources like network socket depending on the class loader implementation . load phase involve the 3 different types of class loader . various class loader

BootStrap class loader ,Extension class loader ,Application class loader, BootStrap loader is responsible for loading internal classes so where does internal class resides . its inside rt.jar that is distributed with your jvm implementation . Extension class loader gets executed which is responsible of loading additional applications jars that is present in jre Lib ext folder whatever the jar present in this folder extension class load and read all the .class files from those jars and give it for further processing.Application class loader loads classes from value specified in you class path specification environment variable and if you specify –cp parameter to the java command the application loader loads classes from that –cp parameter as well.

Link phase Is where a lots of work is done its also has 3 different phases verify,prepare,resolve verify phase basically looks at the byte code that is loaded by the class loader and checks if that is compatible to the JVM class specification or it valid java byte code. For check its use magic number check etc .so that is a class verification when its verified then prepare steps happen. Prepare steps is the place where memory is allocated for the static variable inside a classifier so the class level variable memory is allocated in the prepare phase . Resolve phase is the place where all the symbolic refernces inside the current class are resolved suppose you have reference of 2 another class these are changed from symbolic to the actual reference so though its like depicted in a serial fashion .

So the next phase is initialize Anything under that static block is executed at the initialize phase and wherever you have set values for static variable those values are set into the memory location at this phase.

ClassNotFound exception happens when the class loader fails to find the bytecode corresponding to a class we mentioned .another exception class def not found exception. Generally happens during resolve suppose if class load loads the class A and everything happens correctly verify,prepare but if class A referring the class B and the resolve phase if this .bytecode not found then this exception occurs.



RunTime Data Areas

Method area is the place where the class data eg, static data, byte code etc. so the method area is called aPermGen Space and by default 64 MB is allocated for method area so this can be tuned suppose your application is huge server application that loads a lots of classes in that case you can tuned the perm gen Using –XX: MaxPermSize MB or GB so suppose your trying to load the million sof classes but you never talked about the maxPerm size suppose you left it 64 MB so your application might run into java.lang AutoMemory error for permgenspace in java 8 this permgen called metaspace what they have done they move the method area into the separate memory in the native OS that is called the metaspace so by default here is no limit to meta space in java 8 .



Heap

A a =new A();

Object a is created in the heap so anything to do with the object is created in the heap all the instance variables arrays since arrays are objects are created into the heap. It is a very important area In java that most of the time is tuned based on the application requirement and that is tuned using –Xms for min size and –Xmx for max size by default its ¼ of the physical memory the xms you can tuned as your parameter and adjusts the application needs.

PC register is basically programmed counter register so it contains program counter which is pointing to the next instruction to be executed per thread suppose if there are 3 threads are created and for t1 pointing to next execute instruction and s for t2 and t3 as well .

Java stack contains stack frame corresponding to the current method execution per thread suppose we have thread that executing 3 methods so the java stack for that thread for t1 t2 and t3 respectively in Java stacks so t1 has 3 method executing currently

Stack frame contains data related to the currently executing method Like parameter ,return value,local variable,operand stack which is the place where the scratch area for operation specifiying in the method for the method when any method invoke native method then native method come into the picture. Suppose you want to loadd .dll and run something from dll from your java application which is possible so at that point native method stack will come into the picture stack frame for each invocation will be pushed into the stacks If we don’t give any termination condition and its recursively call the stack frame of stack frame over stack frame then this data are is running out of the memory then we get exception java.lang.StackOverflow error.



Execution engine

Once data area is loaded current instruction to be executed is ready what happens is that the java interpreter interprets the currents instruction that is there in the bytecode and executes it.

Execution engine has following 4 components :

Interpreter take the bytecode instruction looks at it finds out what native operation has to b done and executes that native operation that is done by using native method interface which interface the native method libraries that are present in the jvm so if you look in te jre folder you will see if you are on windows you will see a lots of .dll files so those are the platform specific native libraries that is used by the execution engine. And if you on the unix system you would see .so or .a type of module in jre bin folder which is the native library.

JIT compiler(just in time) what it does suppose we have certain set of instruction that are getiing executed all the time repeatedly those will not interpreted again instead what happens is the JIt compiler will now on the fly compiles this set of instruction and keeps the target machine code ready for the execution so there is no more interpretation involved here so its only machine code executions when its like you know there are so called hotspot that keeps on repeating in the application so you know that’s what the hotspot profiler does it just keep an eye on the byte code that are running and grabs a lot of different statistics from the those byte code that is useful in various format and in various ways it can be used .it helps the jit compiler to compile frequently used instructions .

Garbage collection in Java Java provides automatic memory management thorugh a program called Garbage collector. “Remove Objects that are not used anymore “