Preface to java virtual machine and architecture

Let’s learn preface to java virtual machine and architecture.

preface to java virtual machine and architecture

Every fresher java programmer/developer is aware of Java Virtual Machine which interprets compiled java code known as byte code and excutes it to platform specific machine code.

Java applications are WORA(Write Once Run Anywhere). It is compiled and interpreted.Java compiler complies source code to bytecode.

Then at runtime Java Virtual Machine interprets this bytecode and creates machine code which is executed by machine to run java program.


Preface to java virtual machine and architecture

When we write a java program we use java compiler to compile and get the dot class (.class) file or bytecode as a output.

Now our application consists of .class (dot class) file which is a sub-output that is generated by the java compiler.

It is loaded into first main subsystems of JVM, Class Loader Subsystem. The class loader subsystem is responsible for loading java dynamic class.

Also read – class and objects in java

Class loader subsystem loads, links and initializes class file for the first time at runtime.

Now it should be loaded to perform some operations with respect to other classes which are associated with the JVM.

By using the class loader subsystem the class files are been loaded.

  • loading
  • linking
  • initializing

Loading:

Loading is a phase where class files are loaded. Basically loading is of three.

  1. bootstrap class loader
  2. application class loader and
  3. extension class loader.

Above class loaders follow Delegation Hierarchy Algorithm while loading class files,

Bootstrap Loader:

Bootstrap loader is responsible for loading classes from bootstrap classpath called rt.jarYou often encounter rt.jar file in java directory which consists of all important classes and packages which are required by java.

Extension class loader:

Extension class type loader is responsible for loading important classes which are inside ‘ext’ folder (jre\lib).

Application ClassLoader:

Application ClassLoader are responsible for loading Application Classpath level, mentioned Environment Variable path etc.


Linking:

Linking is the phase where most of the work is done. Linking involves three sub process which are,

  1. verifying,
  2. preparing and
  3. resolving.

Verifying:

Verify phase is a phase where java bytecode is taken care of. It basically checks bytecode whether it is compatible to JVM specification or not.

Preparing:

In prepare phase all static variables memory initialized to their default value.

For example,

public static boolean bool = false;

In prepare phase variable “bool” which is of boolean type will be initialized to the default value of boolean type which is false not true.

Also read – static keyword in java

Because prepare phase involves initializing with default value not with original value.

Resolving:

Now resolve phase job is to replace all symbolic memory references with original references.


Initialization

After resolve phase, third phase comes into play which is initialization. In initialization phase all static variables are assigned with original values and static block is executed.

Runtime Data Area

Runtime data area consists of major components they are,

  1. Method area
  2. Heap area
  3. Stack area
  4. PC registers
  5. Native method stacks

Method area:

Method area stores all class data including static variables. Method area is a shared resource where JVM can have only one method area.

Heap area:

Heap area basically stores object and instance variables. For example, you can store arrays. Since arrays are also objects.

Even methods share memory in heap area. Data stored in heap area is not thread safe (because shared resource). Like method area, there is only one heap area in JVM.

Stack area:

Stack area contain stack frame. It is basically per method invocation. Here every thread has a seperate runtime stack.

Also read – method in java

Stack area is not a shared resource hence thread-safe. Stack memory contains all local variables created. Stack frame is divided into three sub categories,

  1. Local variable array – local variables and corresponding values are stored
  2. Operand stack – acts as runtime workspace for in-between operation.
  3. Frame data – stores symbols relative to method.

PC Registers:

PC registers are basically program counter registers which point to next instruction to be executed.

PC register is responsible for per thread management. Suppose there are three threads, one, two and three. Thread one counts instruction for thread two, thread two counts instruction for thread three.

It holds address of current executing instruction and will be updated with next instruction.

Native Method Stack:

Native method stack works parallelly with java stack which holds information related to native method stack. In java, every thread has one seperate native method stack.


Execution Engine:

Execution Engine is responsible for executing bytecode instruction. It reads bytecode line by line. Execution engine basically comprises of various subsystem namely,

  1. Interpreter,
  2. Garbage collection
  3. Just-In-Time compiler

Interpreter – interprets bytecode instruction line by line. It checks whether bytecode instructions are compatible to execution engine or not. Drawback of interpreter is when one program is called several times each time a new definition is needed.

Just-In-Time compiler – Now here comes picture of just-in-time compiler. Whenever execution engine encounter similar kind of instruction to be executed again and again, what JIT does is it compiles entire bytecode and changes it to native code.

Also read – variables in java

So that it can improve performance in later time. For example, if it encounters XYZ XYZ XYZ multiple times; what JIT does is, JIT pre-compiles XYZ automatically. Now in next instruction if XYZ is encountered, JVM reduces that time, thus leading to performance improvisation.

  • Target code generator : generates native code.
  • Intermediate code generator : generates intermediate code.
  • Code optimizer : optimizes intermediate code.
  • Hotspot profiler : detects hotspots meaning to check whether method is called many times or not.

Garbage Collector – garbage collector removes unused objects by calling System.gc() method.

Java Native Interface – this interface communicates with native method libraries and renders it to execution engine.

Native Method Library – combination of native libraries required for execution engine.

This is all about preface to java virtual machine and architecture.