Tuesday, 18 December 2012

Java Virtual Machine


What is a Java Virtual Machine?

DEFINITION- A Java Virtual Machine (JVM) is a software implementation of the Java Virtual Machine Specification. It interprets compiled Java binary code (called bytecode) for a computer's processor (or "hardware platform") so that it can execute a Java program's instructions. Java was designed to allow application programs to be built that could be run on any platform without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible because it is aware of the specific instruction lengths and other particularities of the platform

The Lifetime of a Java Virtual Machine

A runtime instance of the Java virtual machine has a clear mission in its lifetime: to run one Java application. When a Java application starts, a runtime instance is born. When the application completes, the instance dies. If we start three Java applications at the same time, on the same computer, we will get three Java virtual machine instances. Each Java application runs inside a separate instance of Java virtual machine.
A Java virtual machine instance starts running its solitary application by invoking the main() method of the specified class. The main() method must be public, static, return void, and accept one parameter: a String array. Any class with such a main() method can be used as the starting point for a Java application.

 

The Architecture of the Java Virtual Machine

 

Each Java virtual machine has a class loader subsystem: a mechanism for loading types (classes and interfaces) given fully qualified names.
Each Java virtual machine also has an execution engine: a mechanism responsible for executing the instructions contained in the methods of loaded classes.


The internal architecture of the Java virtual machine.
When a Java virtual machine runs a program, it needs memory to store many things, including bytecodes and other information it extracts from loaded class files, objects the program instantiates, parameters to methods, return values, local variables, and intermediate results of computations. The Java virtual machine organizes the memory it needs to execute a program into several runtime data areas.
Each instance of the Java virtual machine has one method area and one heap. These areas are shared by all threads running inside the virtual machine. When the virtual machine loads a class file, it parses information about a type from the binary data contained in the class file. It places this type information into the method area. (i.e. the information about the loaded classes ) As the program runs, the virtual machine places all objects the program instantiates onto the memory area called heap.

As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. A thread's Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas.

The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method.

The Java virtual machine has no registers to hold intermediate data values. The instruction set uses the Java stack for storage of intermediate data values.

No comments:

Post a Comment