Chapter 18 - Java Virtual Machine Flashcards

1
Q

What is a Virtual Machine?

A

A virtual machine is a virtual representation of a physical computer. We can call the virtual machine the guest machine, and the physical computer it runs on is the host machine.

seee article:
https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Java Virtual Machine?

A

JVM creates an isolated space on a host machine. This space can be used to execute Java programs regardless of the platform or operating system of the machine.
The JVM translates Java bytecode into machine code specific to the underlying hardware, making Java applications platform-independent and enabling the “write once, run anywhere” paradigm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is Java compiled or interpreted language?

A

Java uses a combination of both techniques. Java code is first compiled into byte code to generate a class file. This class file is then interpreted by the Java Virtual Machine for the underlying platform. The same class file can be executed on any version of JVM running on any platform and operating system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Architecture of JVM?

A

The JVM consists of three distinct components:

1) Class Loader
2) Runtime Memory/Data Area
3) Execution Engine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Class Loader and Architecture of it?

A

When you compile a .java source file, it is converted into byte code as a .class file. When you try to use this class in your program, the class loader loads it into the main (RAM) memory.

The first class to be loaded into memory is usually the class that contains the main() method.

There are three phases in the class loading process: loading, linking, and initialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Whats happening before code goes to class loader?

A

Compilation: You write your Java code in a text file with a .java extension. This file contains human-readable Java source code. When you’re ready to run your program, you compile this .java file using a Java compiler (such as javac). The compiler translates the high-level Java code into bytecode instructions. Bytecode is a set of instructions understood by the Java Virtual Machine (JVM).

Bytecode Generation: The Java compiler generates bytecode instructions specific to the Java Virtual Machine (JVM). These instructions are stored in a file with a .class extension, commonly referred to as a class file. Each .class file contains the bytecode for a single Java class.

Class Loading: When you run your Java program, the JVM is responsible for loading the necessary class files into memory. This process is called class loading. The class loader locates the bytecode files (.class files) for the classes referenced in your program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Whats Happening during the first phase: Loading?

A

Loading
Loading involves taking the binary representation (bytecode) of a class or interface with a particular name, and generating the original class or interface from that. This process involves understanding the structure and behavior defined by the bytecode and creating corresponding data structures and code segments in memory that represent the class or interface accurately.

The JVM uses the ClassLoader.loadClass() method for loading the class into memory. It tries to load the class based on a fully qualified name.

If a parent class loader is unable to find a class, it delegates the work to a child class loader. If the last child class loader isn’t able to load the class either, it throws NoClassDefFoundError or ClassNotFoundException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which Built in class loaders available in Java?

A

There are three built-in class loaders available in Java:

Bootstrap Class Loader - This is the root class loader. It is the superclass of Extension Class Loader and loads the standard Java packages like java.lang, java.net, java.util, java.io, and so on. These packages are present inside the rt.jar file and other core libraries present in the $JAVA_HOME/jre/lib directory.

Extension Class Loader - This is the subclass of the Bootstrap Class Loader and the superclass of the Application Class Loader. This loads the extensions of standard Java libraries which are present in the $JAVA_HOME/jre/lib/ext directory.

Application Class Loader - This is the final class loader and the subclass of Extension Class Loader. It loads the files present on the classpath. By default, the classpath is set to the current directory of the application. The classpath can also be modified by adding the -classpath or -cp command line option.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Whats Happening during the second phase: Linking?

A

After a class is loaded into memory, it undergoes the linking process. Linking a class or interface involves combining the different elements and dependencies of the program together.

Linking includes the following steps:

Verification: This phase checks the structural correctness of the .class file by checking it against a set of constraints or rules. If verification fails for some reason, we get a VerifyException.

For example, if the code has been built using Java 11, but is being run on a system that has Java 8 installed, the verification phase will fail.

Preparation: In this phase, the JVM allocates memory for the static fields of a class or interface, and initializes them with default values. During the preparation phase, JVM allocates memory for the variable enabled and sets its value to the default value for a boolean, which is false.

Resolution: In this phase, symbolic references are replaced with direct references present in the runtime constant pool.

For example, if you have references to other classes or constant variables present in other classes, they are resolved in this phase and replaced with their actual references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Whats Happening during the second phase: Initiialization?

A

Initialization involves executing the initialization method of the class or interface (known as <clinit>). This can include calling the class's constructor, executing the static block, and assigning values to all the static variables. This is the final stage of class loading.</clinit>

For example, when we declared the following code earlier:

private static final boolean enabled = true;
The variable enabled was set to its default value of false during the preparation phase. In the initialization phase, this variable is assigned its actual value of true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What components are in Runtime Data Area?

A

1) Method Area
2) Heap Area
3) Stack Area
4) PC Register
5) Native Method Stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Method Area?

A

All the class level data such as the run-time constant pool, field, and method data, and the code for methods and constructors, are stored here.

If the memory available in the method area is not sufficient for the program startup, the JVM throws an OutOfMemoryError.

For example, assume that you have the following class definition:

public class Employee {

private String name;
private int age;

public Employee(String name, int age) {

this.name = name;
this.age = age;   } } In this code example, the field level data such as name and age and the constructor details are loaded into the method area.

The method area is created on the virtual machine start-up, and there is only one method area per JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Runtime Constant Pool Inside the Method Area?

A

The runtime constant pool is a special area within the method area of the Java Virtual Machine (JVM) where it stores constant pool entries. These entries consist of symbolic references used by the class’s bytecode instructions.

Here’s a breakdown of what’s stored in the runtime constant pool:

Symbolic References: These are references to classes, methods, fields, and other constants used by the class. For example, when a method in one class calls a method in another class, the method’s name, descriptor (signature), and class name are stored as symbolic references in the runtime constant pool.

String Constants (String Pool): String literals used in the class are also stored in the runtime constant pool. For example, if a class contains the string literal “Hello, world!”, a reference to this string is stored in the runtime constant pool.

Numeric and Other Constants: Numeric constants such as integers, floats, longs, doubles, and other constants like class literals are also stored in the runtime constant pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Heap Area?

A

All the objects and their corresponding instance variables are stored here. This is the run-time data area from which memory for all class instances and arrays is allocated.

For example assume that you are declaring the following instance:

Employee employee = new Employee();
In this code example, an instance of Employee is created and loaded into the heap area.

The heap is created on the virtual machine start-up, and there is only one heap area per JVM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Stack Area?

A

Stack Area
Whenever a new thread is created in the JVM, a separate runtime stack is also created at the same time. All local variables, method calls, and partial results are stored in the stack area.

If the processing being done in a thread requires a larger stack size than what’s available, the JVM throws a StackOverflowError.

For every method call, one entry is made in the stack memory which is called the Stack Frame. When the method call is complete, the Stack Frame is destroyed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Program Counter (PC) Registers

A

The JVM supports multiple threads at the same time. Each thread has its own PC Register to hold the address of the currently executing JVM instruction. Once the instruction is executed, the PC register is updated with the next instruction.

17
Q

What is Native Method Stacks?

A

The JVM contains stacks that support native methods. These methods are written in a language other than the Java, such as C and C++. For every new thread, a separate native method stack is also allocated.

18
Q

What is Execution Engine?

A

Once the bytecode has been loaded into the main memory, and details are available in the runtime data area, the next step is to run the program. The Execution Engine handles this by executing the code present in each class.

However, before executing the program, the bytecode needs to be converted into machine language instructions. The JVM can use an interpreter or a JIT compiler for the execution engine.

It takes the bytecode, which is a set of platform-independent instructions generated by compiling Java source code, and translates them into actions that the underlying hardware can execute.

19
Q

Architecture of Execution Engine?

A

Interpreter, JIT Compiler
Garbage Collector

20
Q

Interpreter?

A

The interpreter reads and executes the bytecode instructions line by line. Due to the line by line execution, the interpreter is comparatively slower.

Another disadvantage of the interpreter is that when a method is called multiple times, every time a new interpretation is required.

21
Q

JIT Compiler?

A

The JIT Compiler overcomes the disadvantage of the interpreter. The Execution Engine first uses the interpreter to execute the byte code, but when it finds some repeated code, it uses the JIT compiler.

The JIT compiler then compiles the entire bytecode and changes it to native machine code. This native machine code is used directly for repeated method calls, which improves the performance of the system.

The JIT Compiler has the following components:

Intermediate Code Generator - generates intermediate code
Code Optimizer - optimizes the intermediate code for better performance
Target Code Generator - converts intermediate code to native machine code
Profiler - finds the hotspots (code that is executed repeatedly)

22
Q

Java Native Interface (JNI)

A

Java Native Interface (JNI)
At times, it is necessary to use native (non-Java) code (for example, C/C++). This can be in cases where we need to interact with hardware, or to overcome the memory management and performance constraints in Java. Java supports the execution of native code via the Java Native Interface (JNI).

23
Q

Native Method Libraries

A

Native Method Libraries are libraries that are written in other programming languages, such as C, C++, and assembly. These libraries are usually present in the form of .dll or .so files. These native libraries can be loaded through JNI.