Chapter 18 - Java Virtual Machine Flashcards
What is a Virtual Machine?
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/
What is the Java Virtual Machine?
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.
Is Java compiled or interpreted language?
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.
Architecture of JVM?
The JVM consists of three distinct components:
1) Class Loader
2) Runtime Memory/Data Area
3) Execution Engine
What is Class Loader and its phases?
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.
Whats happening before code goes to class loader?
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.
Whats Happening during the first phase: Loading?
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.
Which Built in class loaders available in Java?
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.
Whats Happening during the second phase: Linking?
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.
Whats Happening during the next phase: Initiialization?
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.
What components are in Runtime Data Area?
1) Method Area
2) Heap Area
3) Stack Area
4) PC Register
5) Native Method Stack
What is Method Area?
The method area stores class-level data, including information about class structures, methods, fields, the code for methods, including constructors and static initializers, and the runtime constant pool.
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.
What is Runtime Constant Pool Inside the Method Area?
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.
What is Heap Area?
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.
What is Stack Area?
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.