Wednesday, July 23, 2014

The Java Virtual Machine (JVM)



What is the Java Virtual Machine (JVM), and what it does?

First of all, you should read this article about Java.

Java is a high-level language, which means that is closer to our natural language than machine code, the set of instructions a computer uses to construct and run a program.

Java programming language was designed at SUN, by James Goslin, to allow programmers to write code that could run on any platform without the need for rewriting or recompilation for each separate platform.

A Java virtual machine makes that possible, by defining an abstract -- not a real -- machine or processor that translates the Java code to instructions that a specific platform understands. It interprets compiled Java binary code (bytecode) and translates it for a computer's processor (or "hardware platform") so that it can perform a Java program's instructions.

A Java Virtual Machine is an implementation of the Java Virtual Machine Specification, which is created and reviewed by the Java Community Process (JCP).  The Specification defines an instruction set, a set of registers, a stack, a "garbage heap", and a method area.


JVM architecture
In a Java program, the source code is compiled to Java bytecode, which is verified, interpreted or JIT-compiled for the native architecture.
A JVM can either interpret the bytecode one instruction at a time (mapping it to a real processor instruction) or the bytecode can be fully compiled for the real processor using what is called a just-in-time (JIT) compiler, which normally achieves a greater execution speed.
The Java APIs and JVM together make up the Java Runtime Environment (JRE).

When a Java project is built, the source code (*.java source files) is translated to Java bytecode (*.class files), taking high-level code one step closer to the machine code. The bytecode is a collection of compact instructions, easier for a machine to interpret.
When running a Java application on a computer, cellphone, or any other Java-enabled platform, the Java bytecode is passed to the Java Virtual Machine. The interpreter in the JVM usually starts compiling the entire bytecode at runtime, following the principles of just-in-time (JIT) compilation.


JVM advantages
The main advantage in using a JVM is the compatibility. Since applications run in a virtual machine instead of directly on the hardware, the developer can program and build the application once, and executed it on every device that has a Java Virtual Machine. This principle has given birth to the Java slogan: “Write once, run everywhere.”

But there are other benefits.
One of the most important is the relative security of Java programs, as a result of the Java Virtual Machine. A program running in a virtual machine is far less likely to disrupt the user’s operating system, or corrupt data files, if errors occur. Also, JVM has an efficient way to achieve memory protection.


Heap
The heap is the area of memory used by the JVM. The heap is divided into generations:
 - Young Generation: stores short-lived objects that are created and immediately garbage collected.
 - Old Generation (or Tenured Generation): objects that persist longer are move to this area.

The Permanent Generation (permgen) was removed in Java 8. It was used for class definitions and associated metadata. Permanent generation was not part of the heap. Originally there was no permanent generation, and objects and classes were stored together in the same area. But as class unloading occurs much more rarely than objects are collected, moving class structures to a specific area allowed significant performance improvements.

For more information on this subject, please read the JVM Memory Structure article. (to be created)


Most known JVMs
Creator
Name
Description
Oracle
Hotspot (*)
Originally developed by Animorphic, a company that was acquired by Sun in 1997. Sun was acquired by Oracle in 2009.
It is a Java virtual machine for desktops and servers, maintained and distributed by Oracle. It features techniques such as just-in-time compilation and adaptive optimization designed to improve performance.
Oracle
JRockit (*)
It was originally developed by Appeal, a Swedish company that was acquired by BEA in 2002. BEA became part of Oracle in 2008.
This JVM was created with the intention of being used for  optimized for large applications requiring long running tasks, a lot of memory and a scalable environment.
IBM
J9
It is the basis of multiple IBM Java offerings, including WebSphere and the IBM JDK.
This JVM aimed at portability to different platforms, from mobile phones to zSeries mainframes.

(*) Hotspot and JRockit will be integrated in a single JVM release.  For more on this issue, read this article from “Henrik on Java” blog: https://blogs.oracle.com/henrik/entry/oracles_jvm_strategy


References
 - Oracle’s Hotspot JVM: http://en.wikipedia.org/wiki/HotSpot
 - Oracle’s JRockit JVM: http://en.wikipedia.org/wiki/JRockit
 - Java home: http://java.com
 - Oracle’s Java Tech page: http://www.oracle.com/technetwork/java/index.html
 - Java and JVM specifications: http://docs.oracle.com/javase/specs/

No comments:

Post a Comment