Breaking News

Saturday, 14 May 2016

Java Virtual Machine

A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program.An implementation of JVM specification, interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions.

There are three notions of the JVM: specification, implementation, and instance.

  1. Specification:- The Specification is a document specifies an instruction set, a set of registers, a stack, a "garbage heap," and a method area. where working of Java Virtual Machine is specified. But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies. Anybody is free to take that spec and build their own jvm implementation.Think of specification like a blueprint and implementation is like the house you built from it.
  2. An implementation It provides an implementation of a jvm based on jvm specification. Once a Java virtual machine has been implemented for a given platform, any Java program (which, after compilation, is called bytecode) can run on that platform and this implementation is known as JRE(Java Runtime Environment.) JRE includes a Java Virtual Machine implementation together with an implementation of the Java Class Library.
  3. Runtime Instance: It provides an instance of jvm implementation. A Java virtual machine can either interpret the bytecode one instruction at a time (mapping it to a real processor instruction) or the bytecode can be compiled further for the real processor using what is called a just-in-time compiler. Whenever you write java command on the command prompt to run the java class, and instance of JVM is created. Different companies have their own implementations of the JVM runtime from the spec.
The JVM Loads Code, Verifies Code, Executed Code, Provide Runtime Environment and provides definitions for the memory area, class file format, Register set, Garbage-Collected heap, Fatal error reporting etc.

JVM provides definitions for the:-

  • Class Loader:- A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
  • Method Area:-  When the Java virtual machine loads a type, it uses a class loader to locate the appropriate class file. The class loader reads in the class file--a linear stream of binary data--and passes it to the virtual machine. The virtual machine extracts information about the type from the binary data and stores the information in the method area. Memory for class (static) variables declared in the class is also taken from the method area. It basically stores:-

Type's Information:-
  • Fully qualified name of the type  
  •  Type's superclass fully qualified type name 
  •  The type's modifers such as public, protected, final, abstract, etc. 
  •  An ordered list of the fully qualified names of any direct superinterfaces. 
In addition to Type's Information it also stores:-
  • The Constant pool for the type. 
  •  Methods and Fields Information. 
  •  Classes Static and instance variables, except Constants. 
  •  A reference to Class Loader. 


  • Heap:-In running program whenever a new object is created the memory used is allocated from the heap. Virtual Machine also provides the Garbage Collector to collect the allocated memory when object execution completes.
  • Java Stack:- When a new thread is launched, the Java virtual machine creates a new Java stack for the thread. The Java virtual machine only performs two operations directly on Java Stacks: it pushes and pops frames. Java Stack stores frames.It holds local variables and partial results, and plays a part in method invocation and return. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
  • PC Registers:-It contains the address of the java virtual machine instruction currently being executed.
  • Native Method Stack:-  When a thread invokes a native method, it enters a new world in which the structures and security restrictions of the Java virtual machine no longer hamper its freedom. A native method can likely access the runtime data areas of the virtual machine (it depends upon the native method interface), but can also do anything else it wants. It may use registers inside the native processor, allocate memory on any number of native heaps, or use any kind of stack.
  • Execution Engine:-It contains: A virtual Processor, interpreted and Just In Time Compiler (JIT).




No comments:

Post a Comment

Designed By Published.. Blogger Templates