“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

In this post, we will discuss the JVM trivia. You’ll learn about the architectural design modules of the JVM, and we’ll briefly describe what the JVM is and how it works. In addition, we will talk in detail about the loader, runtime data area, and execution engine, which are the core components of the JVM.

What is the JVM?

It is well known that we need to compile or interpret any source code before executing it. But Java uses a combination of both, a compiler and an interpreter.

Java programs are first compiled into Java bytecode (binary form), and then a special Java interpreter interprets them for a particular platform.

Java ByteCode is the machine language of the Java Virtual Machine (JVM). The JVM converts compiled binary bytecode to a specific machine language.

The Java virtual machine acts as a sub-part of the Java Runtime environment (JRE).

The JVM is an abstract machine that works on top of an existing process. We can do it with hardware or software.

The JVM, combined with the Java API, constitutes the Java platform. The basic use of the JVM is HotSpot.

We can define a JVM in three ways:

1. Specify the working specifications of the Java VM. The person providing the implementation can choose the algorithm independently.

2. An implementation called the Java Runtime environment (JRE).

3. A runtime instance, which creates a JVM instance when you write commands at a command prompt to run Java classes.

The work of the JVM

The JVM (Java Virtual Machine) acts as a runtime engine to run Java applications. The JVM calls the main method that exists in Java code.

The Java Virtual Machine (JVM) is part of the JRE (Java runtime environment).

Java applications are WORA (written once, run anywhere). This means we can write a Java program once and run it on different platforms without making any changes to the Java program.

When we compile a.java file, the compiler generates a.class file (containing bytecode) with the same name as the class that exists in the.java file.

When we run a.class file, it goes through different steps. The following diagram shows the flow of the next Java file from compilation to execution:

Why you need a JVM

The JVM exists to handle the tasks that Java programs perform that are necessary to develop programs that involve Java. The Java VM performs the following tasks:

Load, validate, execute code;

Provide the runtime environment, a memory area, register set, garbage collection heap;

Report fatal errors and provide a class file format

These tasks make the Java virtual machine an integral part of Java development.

Architecture design for the JVM

There are three main submodules in the JVM architecture:

1. Class loader

2. Runtime data

3. Execution engine

The following is the JVM architecture diagram:

Read more about the details of each section

Java class loader

It is a component of the JVM architecture used to load classes in memory. Each JVM contains a ClassLoader. The three built-in class loaders in Java are:

1. Boot the classloader. This is the class loader, which is a superclass that extends the class loader. It loads the rt.jar file.

2. Extend the class loader. It is the ClassLoader that loads the JAR files that exist in the directory. This is the parent of the children Bootstrap classLoader and System classLoader.

3. System/application class loader. ClassLoader loads class files from the class path. This is a subclass of the extension classloader.

The three important functions of ClassLoader are initialization, loading, and linking.

1. Initialization

This operation involves assigning all static variables their specific values in a block. At this stage, all static variable assignments are defined in code and static blocks. Execute from top to bottom in a class and from parent to child in a class hierarchy.

2. Load

This operation loads files from secondary memory into main memory (RAM) for execution. The Classloader reads the. Class file and generates the corresponding binary data, which is stored in the method area.

The JVM stores some information about each.class file in the method area. The messages are:

The fully qualified name of the loaded class and its immediate parent.

Whether a.class file is associated with an interface, enumeration, or class.

Modifiers, variables, method information, etc.

After the.class file is loaded, the JVM creates an object of type class in heap memory to represent the file. Programmers can use this Class object to obtain class-level information, such as the name of the Class, parent name, method, and variable information. To get this Object reference, we can use the getClass() method of the Object class.

Links to 3.

This combines different files in the main program. It performs validation, preparation, and (optionally) parsing.

Validation: The validation phase checks the correctness of the.class file. This means that it checks that file formation and generation is done by a valid compiler. If the validation fails, we get a java.lang.Verify exception.

Preparation: The JVM allocates memory for class variables and initializes memory to default values.

Parsing: Parsing is the process of replacing symbolic references with direct references. It uses the search method area to locate referenced entities.

The JVM memory area

1. Method area – this stores the structure of each class, such as method data, field data, runtime pool, metadata.

2. The heap — the heap is the run-time area where object allocation occurs.

3. Stack — The stack stores partial results and local variables of a program. Whenever a thread is created, the JVM stack is created at the same time. When we call a method, a new framework is created and destroyed as the calling process completes.

4. PC register — This stores the address of currently executing JVM instructions.

5. Local method stack – This contains all the local methods required by any application. It is not written in Java.

Execution engines in Java

It is the component of the JVM that reads data from a memory location and executes instructions. It has three main components, namely a virtual processor, an interpreter, and a JIT compiler.

1. Virtual processors

2. Interpreter: Reads the bytecode stream and executes the instructions.

3. Just-in-time (JIT) compiler: It improves performance. JIT compiles parts of the bytecode with similar functionality simultaneously and reduces the compilation time.

Local method interface

It is a framework that facilitates communication between different applications written in different languages such as C, C ++, and so on. The native method interface allows Java code running in the JVM to be invoked by libraries and native applications.

Local method library

This library is the execution engine essential library (C, C++) collection.

Differences and connections between the JDK, JVM, and JRE

1. JVM represents the Java VIRTUAL machine, JDK represents the Java development package, and JRE represents the Java runtime environment.

2. The JDK is used in the development environment, while the JRE is used in the runtime environment.

3. The JVM runs in the JRE environment. The JRE contains class libraries, the Java Virtual machine, and other files in addition to development tools such as compilers and debuggers.

4. JRE is a subset of the JDK. The JDK contains the JRE and development tools such as compilers and debuggers.

Key knowledge points

The JVM stands for the Java virtual machine that drives Java code. It translates Java bytecode into machine language.

Here, Java code is compiled into bytecode. This bytecode is interpreted on different machines

JIT stands for just-in-time compiler. JIT is part of the Java Virtual machine. It increases execution time

Java can be slow to execute compared to other compiler machines,

conclusion

In this article we briefly looked at the architectural design of the Java Virtual Machine and its module content. In the next article, we’ll take a closer look at the core workflow of the class loader.

References:

1. Deep Understanding of JVM Virtual Machines

2, techvidvan.com/tutorials/j…