Java Virtual Machine (JVM)
is a engine that provide runtime environment to drive the Java code or applications. It converts Java bytecode into machines language. JVM is a part of Java Run Environment (JRE). In Other programming languages, the compiler produces machine code for a particular system. However, Java compiler produces code for a Virtual Machine known as Java Virtual Machine.
Here is how JVM works
- First, Java code is complied into bytecode. This bytecode gets interpreted on different machines.
- Between host system and Java source, Bytecode is an intermediary language.
- JVM is responsible for allocating memory space.
Components of the JVM are:
- Class Loaders
- Run-Time Data Areas (JVM Memory)
- Execution Engine
The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading, Linking, and Initialization.
JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for methods.
All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common and shared across multiple threads. See Java Memory.
JVM language Stacks
Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created simultaneously (at the same time) as the thread is created. A new frame is created whenever a method is invoked, and it is deleted when method invocation process is complete.
PC register store the address of the Java Virtual machine instruction which is currently executing. In Java, each thread has its separate PC register.
Native Method Stacks
Native method stacks hold the instruction of native code depends on the native library. It is written in another language instead of Java.
It is type of software used to test hardware, software, or complete systems. The test execution engine never carries any information about the tested product.
Native Method Interface
The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to call by libraries and native applications.
Native Method Libraries Native Libraries is a collection of the Native Libraries (C, C++) which are needed by the Execution Engine.
Run-Time Data Areas
Execution engine execute the
.class (bytecode). It reads the byte-code by line, use data nad information present in various memory area and execute instructions.
It can be classified in three parts:
- Interpreter: It interprets the bytecode line by line and then executes. The disadvantage here is that when one method is called multiple times, every time interpretation is required.
- Just-In-Time Compiler (JIT): It is used to increase efficiency of interpreter. It compiles the entire bytecode and changes it to native code so whenever interpreter see repeated method calls, JIT provide direct native code for that part so re-interpretation is not required, thus efficiency is improved.
- Garbage collector: It destroy un-referenced objects.
Memory exception and error
- Java Virtual Machine (JVM) & its Architecture
- How JVM Works - JVM Architecture? - GeeksforGeeks
- Difference Between JVM, JRE, and JDK - Baeldung
- Java Virtual Machine JVM tutorial - JVM tutorial - Inside JVM
- jvm系列(二):JVM内存结构 - 纯洁的微笑 - 博客园