Converted into the previous clean HTML study-notes format. Original PDF Pages are intentionally not included.
1. Virtual Machine
A Virtual Machine (VM) is software that emulates a computer system and provides an environment for running applications without requiring the application to interact directly with the physical hardware.
A VM can provide an isolated environment in which applications or operating systems can execute while sharing the underlying host resources.
2. Types of Virtual Machines
| Type | Meaning | Main purpose |
|---|---|---|
| Hardware/System Based VM | Provides multiple logical computer systems on one physical host. | Run multiple operating-system environments while sharing hardware resources. |
| Software/Process/Application Based VM | Acts as a runtime engine for a particular application or programming language. | Provide a platform-independent execution environment for a process. |
3. Hardware Based / System Based Virtual Machine
System VMs provide isolated logical systems on the same physical computer. The host's hardware resources can be shared and managed among multiple virtual machines.
Examples
- KVM (Kernel-based Virtual Machine) for Linux
- VMware
- Xen
- Cloud computing environments
Advantages
- Effective utilization of hardware resources.
- Multiple operating systems can coexist on one physical host.
- Host and guest environments can share selected resources and files.
- Developers can work in isolated environments without changing the host operating system.
- Virtualization software can support provisioning, high availability, maintenance and disaster recovery.
4. Software Based / Process Based Virtual Machine
Process-based VMs act as runtime engines for particular programming-language applications. They are also called application VMs or Managed Runtime Environments (MREs).
- They run as normal applications inside the host operating system.
- They generally support a single process.
- They are started when the process starts and destroyed when the process exits.
- They provide a platform-independent execution environment.
Examples
| VM | Runtime purpose |
|---|---|
| JVM | Runs Java applications. |
| PVM (Parrot VM) | Runs Perl-related programs/scripts. |
| CLR | Runtime engine for .NET applications. |
5. Virtual Machine Language / Bytecode
A virtual-machine-oriented language representation can be executed through a VM rather than being tied directly to one operating system's native instruction set.
For Java, source code is compiled into bytecode. The JVM interprets or JIT-compiles that bytecode for the underlying platform.
This is the foundation of Java's write once, run anywhere model: the same bytecode can be executed on platforms for which a compatible JVM exists.
6. JDK, JRE and JVM
| Component | Purpose |
|---|---|
| JDK — Java Development Kit | Used to develop and run Java applications. Contains development tools and a runtime environment. |
| JRE — Java Runtime Environment | Provides the runtime environment and libraries required to run Java applications. |
| JVM — Java Virtual Machine | Executes Java bytecode. |
JRE = JVM + Java Libraries
Conceptually, the JRE is the runtime portion of the JDK, and the JVM is the execution engine inside that runtime environment.
Simple flow
7. JVM Internal Architecture
The JVM is responsible for loading and executing Java applications. Its major runtime components include the Class Loader Subsystem, Runtime Data Areas, Execution Engine, JNI and Native Method Libraries.
The architecture diagram in the source PDF shows class files entering the Class Loader Subsystem, runtime data areas supplying execution state, and the Execution Engine interacting with JNI and native libraries. fileciteturn18file0L13-L17
8. Runtime Data Areas
| Area | Purpose | Scope |
|---|---|---|
| Method Area | Stores class-level information and runtime structures associated with loaded classes. | Per JVM. |
| Heap | Stores objects and arrays created by Java programs. | Per JVM. |
| Java Stack | Stores method invocation frames, local variables, return information and intermediate calculations. | Per thread. |
| PC Register | Indicates the next instruction to execute for the current thread. | Per thread. |
| Native Method Stack | Supports invocation/execution state for native methods. | Per thread. |
9. Stack, Stack Frames and PC Register
Whenever a new thread is created, the JVM provides that thread with its own PC register and JVM stack.
- The PC register identifies the next instruction to execute for the current thread.
- The stack stores method invocations.
- Each method invocation creates a stack frame.
- A frame contains information such as local variables, return information and intermediate calculations.
- When a method completes, its frame is automatically removed from the stack.
Java Stack → Stack Frame 1 + Stack Frame 2 + Stack Frame 3 ...
The source notes explicitly describe PC registers and stacks as thread-specific and explain that every method entry is represented by a stack frame. fileciteturn18file0L21-L35
10. Class Loader Subsystem
The Class Loader Subsystem is responsible for bringing class information into the JVM and making it ready for execution.
Three major activities
- Loading
- Linking
- Initialization
These three responsibilities are listed in the source notes. fileciteturn17file0L119-L123
11. Loading
Loading means reading a class file and storing the corresponding binary information in the Method Area.
For each loaded class, the JVM maintains the class-level metadata needed to represent and use that class. After loading, the JVM creates a corresponding java.lang.Class object on the Heap to represent the class-level binary information.
12. Linking
Linking consists of three activities:
- Verification
- Preparation
- Resolution
13. Verification
Verification checks whether the binary representation of a class is structurally correct and whether the class file was generated by a valid compiler according to JVM rules.
- The bytecode verifier performs this validation.
- If verification fails, the JVM can throw
java.lang.VerifyError.
14. Preparation
During Preparation, the JVM allocates memory for class-level static variables and assigns their default values.
The original explicitly assigned values are applied later during the Initialization phase.
| Phase | Static field state |
|---|---|
| Preparation | Memory allocated and default values assigned. |
| Initialization | Explicit/original initialization values are assigned and initialization logic runs. |
15. Resolution
Resolution converts symbolic references used by the class into direct references to the actual runtime entities.
For example, class names stored in the constant pool can be resolved by locating the corresponding loaded class information in the Method Area.
The source example explains that classes such as Test, String, Student and Object may be loaded, while their names in the constant pool are resolved to actual references during this phase. fileciteturn17file0L158-L167
16. Initialization
Initialization is the phase in which the class is initialized, including assigning explicitly specified static field values and executing applicable class initialization logic.
In simple terms:
During preparation static fields receive default values; during initialization their explicit initialization values are established.
17. Errors During Loading, Linking and Initialization
If an error occurs during the class loading/linking/initialization process, the JVM can report a java.lang.LinkageError or one of its subclasses. The source specifically notes that VerifyError is a child of LinkageError. fileciteturn17file0L172-L173
| Problem | Possible error |
|---|---|
| Invalid bytecode/class structure during verification | VerifyError |
| Other class loading/linkage incompatibilities | LinkageError or subclass |
18. Java ClassLoader
ClassLoader is an abstract class in the java.lang package. It is responsible for finding and loading classes at runtime from available resources.
Key idea
When the JVM needs a class, the class-loading mechanism checks whether the class is already loaded. If not, a ClassLoader hierarchy is used to locate and load it.
19. ClassLoader Principles
Java ClassLoader behavior is commonly explained using three principles:
1. Delegation Principle
A child ClassLoader delegates a class-loading request to its parent first. The child attempts to load the class only if the parent cannot find it.
2. Visibility Principle
A child ClassLoader can see classes loaded by its parent. A parent generally cannot see classes that exist only in a child ClassLoader.
3. Uniqueness Principle
The delegation model helps ensure that a class is not unnecessarily loaded multiple times by different levels of the hierarchy.
20. ClassLoader Delegation Hierarchy
The source notes describe the request flowing from the Application ClassLoader toward the Extension ClassLoader and then the Bootstrap ClassLoader, with the parent searched before the child performs its own loading. fileciteturn17file0L197-L214
jre/lib/ext extension mechanism no longer applies. The delegation concept remains important.21. Class Loading Flow
- JVM encounters a class reference.
- It checks whether the class is already loaded.
- If already loaded, the existing loaded class is reused.
- If not loaded, the Class Loader Subsystem receives the request.
- The Application ClassLoader delegates to its parent.
- The parent hierarchy is searched according to delegation.
- If no parent can load the class, an appropriate child loader attempts to find it.
- If the class cannot be found in the application class path,
ClassNotFoundExceptioncan occur for an application-level lookup.
22. JIT Compiler
JIT (Just-In-Time) Compiler is part of the JVM's execution machinery and is used to improve runtime performance.
Instead of interpreting every bytecode instruction repeatedly, frequently executed code can be compiled into native machine code and reused, reducing execution overhead.
23. JNI and Native Method Libraries
JNI (Java Native Interface) provides a bridge between Java code and native code/libraries.
- Native methods are implemented outside ordinary Java bytecode.
- JNI allows the JVM to interact with native methods.
- Native method libraries provide the native implementation used by those methods.
24. Class File Structure — Magic Number
A Java .class file starts with a well-known magic number: 0xCAFEBABE.
The JVM uses this value to recognize a Java class file and validate that the file has the expected class-file format.
Magic Number:
0xCA FE BA BE
25. Class File Version — Minor and Major Version
Java class files contain minor and major version information. These values identify the class-file format/compiler version expected by the JVM.
This is one reason a class compiled for a newer Java release may not run on an older JVM: the older JVM may reject the unsupported class-file version.
26. Per-JVM vs Per-Thread Areas
| Per JVM | Per Thread |
|---|---|
| Method Area | Java Stack |
| Heap | PC Register |
| Native Method Stack |
The source notes summarize Method Area and Heap as belonging to the JVM, while Stack Area, PC Registers and Native Method Stack are associated with individual threads. fileciteturn17file0L219-L229
27. Complete JVM Execution Flow
28. Interview / Quick Revision
- What is a VM? Software that emulates/provides a managed environment for executing applications.
- What is JVM? The Java Virtual Machine that loads and executes Java bytecode.
- JDK vs JRE vs JVM? JDK is for development; JRE provides runtime libraries/environment; JVM executes bytecode.
- What are ClassLoader phases? Loading, Linking and Initialization.
- What are linking activities? Verification, Preparation and Resolution.
- What happens in Preparation? Memory is allocated for static fields and default values are assigned.
- What happens in Resolution? Symbolic references are resolved to direct references.
- What does a PC register store? The next instruction to execute for a thread.
- What does a stack frame contain? Method invocation information such as local variables, return information and intermediate calculations.
- What is the ClassLoader delegation principle? A child asks its parent to load a class before trying itself.
- What is the class-file magic number?
0xCAFEBABE. - What is JIT? A runtime compiler used to improve execution performance.
- What is JNI? A bridge between Java and native code/libraries.