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.
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.
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.
ConversionConversion EmoticonEmoticon