Saturday, September 13, 2008

The Java Virtual Machine (JVM)

At the heart of the Java platform lies the Java Virtual Machine, or JVM. Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. The difference with Java is that it uses byte code - a special type of machine code.Java bytecode executes on a special type of microprocessor. Strangely enough, there wasn't a hardware implementation of this microprocessor available when Java was first released. Instead, the processor architecture is emulated by what is known as a "virtual machine". This virtual machine is an emulation of a real Java processor - a machine within a machine (Figure One). The only difference is that the virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

Figure - JVM emulation run on a physical CPU


The Java Virtual Machine is responsible for interpreting Java bytecode, and translating this into actions or operating system calls. For example, a request to establish a socket connection to a remote machine will involve an operating system call. Different operating systems handle sockets in different ways -but the programmer doesn't need to worry about such details. It is the responsibility of the JVM to handle these translations, so that the operating system and CPU architecture on which Java software is running is completely irrelevant to the developer.The Java Virtual Machine forms part of a large system, the Java Runtime Environment (JRE). Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM. The portability of Java comes from implementations on a variety of CPUs and architectures. Without an available JRE for a given environment, it is impossible to run Java software.


Now lets have a look at JVM architecture



  1. A virtual machine (VM) is an abstract computer architecture

  2. It is Software on top of a real hardware

  3. Can run the same application on different machines where the VM is available

  4. An abstract computing machine that executes bytecode programs4.1 An instruction set and the meaning of those instructions – the bytecodes4.2 A binary format – the class file format4.3 An algorithm to verify the class file


Interpreter :The Java runtime interpreter (java) is the component of the Java Developer's Kit used to runexecutable Java bytecode classes.Just-in-time compilation :In the Java programming language and environment, a just-in-time (JIT) compiler isa program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor. After you've written a Java program, the source language statements are compiled by the Java compiler into bytecode rather than into code that contains instructions that match a particular hardware platform's processor (for example, an Intel Pentium microprocessor or an IBM System/390 processor).


The bytecode is platform-independent code that can be sent to any platform and run on that platform. The Java on any platform will interpret the compiled bytecode into instructionsunderstandable by the particular processor. However, the virtual machine handles one bytecode instruction at a time. Using the Java just-in-time compiler (really a second compiler) at the particular system platform compiles the bytecode into the particular system code (as though the program had been compiled initially on that platform). Once the code has been (re-)compiled by the JIT compiler, it will usually run more quickly in the computer.The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed. Sun Microsystems suggests that it's usually faster to select the JIT compiler option, especially if the method executable is repeatedly reused.

Fig : JVM Diagram


JVM Architecture :


No comments: