Introduction to JVM


What is JVM?

  • JVM stands for Java Virtual Machine.
  • JVM is the engine that drives the java code.
  • JVM provides runtime environment to execute java byte code.
  • The JVM doesn't understand Java type, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM.
  • JVM control execution of every Java program. It enables features such as automated exception handling, Garbage-collected heap.

Virtual Machine

  • In general terms Virtual machine is a software that creates an environment between the computer and end user in which end user can operate programs.
  • As per functionality Virtual machine is creation of number of different identical execution environments on a single computer to execute programs is called Virtual machine.

Java Virtual Machine

  • Java Virtual Machine (JVM) is an execution environment for Java applications.
  • In the general sense, the JVM is an abstract computing machine defined by a specification, which is designed to interpret bytecode that is compiled from Java source code.

Bytecodes

  • Bytecodes are the machine language of the Java virtual machine.
  • It is a binary data format that includes loading information and execution instructions for the Java virtual machine. In that sense, Java bytecode is a special kind of binary code.

JVM Architecture



  • Class Loader: Responsible for reading Java source code and loading classes into the data areas.
  • Execution Engine: Responsible for executing instructions from the data areas.

Class Loader

The JVM uses different class loaders organized into the following hierarchy:
  • The bootstrap class loader is the parent for other class loaders. It loads the core Java libraries and is the only one written in native code.
  • The extension class loader is a child of the bootstrap class loader. It loads the extension libraries.
  • The system class loader is a child of the extension class loader. It loads the application class files that are found in the classpath.
  • A user-defined class loader is a child of the system class loader or another user-defined class loader.
When a class loader receives a request to load a class, it checks the cache to see if the class has already been loaded, then delegates the request to the parent. If the parent fails to load the class, then the child attempts to load the class itself. A child class loader can check the cache of the parent class loader, but the parent cannot see classes loaded by the child. The design is such because a child class loader should not be allowed to load classes that are already loaded by its parent.

Execution Engine

The execution engine executes commands from the bytecode loaded into the data areas one by one. To make the bytecode commands readable to the machine, the execution engine uses two methods.

  • Interpretation: The execution engine changes each command to machine language as it is encountered.
  • Just-in-time (JIT) compilation: If a method is used frequently, the execution engine compiles it to native code and stores it in the cache. After that, all commands associated with this method are executed directly without interpretation.

Although JIT compilation takes more time than interpretation, it is done only once for a method that might get called thousands of times. Running such method as native code saves a lot of execution time compared to interpreting each command one by one every time it is encountered.

JIT compilation is not a requirement of the JVM specification, and it is not the only technique that is used to improve JVM performance. The specification defines only which bytecode commands relate to which native code; it is up to the implementation to define how the execution engine actually performs this conversion.

Memory Model

The Java memory model is built on the concept of automatic memory management. When an object is no longer referenced by an application, a garbage collector discards it and this frees up memory. This is different from many other programming languages, where you have to manually unload the object from memory.
The JVM allocates memory from the underlying OS and separates it into the following areas.

  • Heap Space: This is a shared memory area used to hold the objects that a garbage collector scans.
  • Method Area:This area was previously known as the permanent generation where loaded classes were stored. It has recently been removed from the JVM, and classes are now loaded as metadata to native memory of the underlying OS.
  • Native Area This area holds references and variables of primitive types.

Threads

The JVM runs in a single process, but it can execute several threads concurrently, each one running its own method. This is an essential part of Java. An application such as an instant messenger client, runs at least two threads; one that waits for user input and one that checks the server for incoming messages. Another example is a server application that executes requests in different threads: sometimes each request can involve several threads running concurrently.

All threads share the memory and other resources available to the JVM process. Each JVM process starts a main thread at the entry point (the main() method). Other threads are started from it and present an independent path of execution. Threads can run in parallel on separate processors, or they can share one processor. The thread scheduler controls how threads take turns executing on a single processor.

Newest
Previous
Next Post »