Java Language Basics - Execution, Memory & JVM
Table of Contentsβ
- Java Overview
- Java Execution Process
- Java Virtual Machine (JVM)
- Memory Management
- Code Compilation & Execution
- Java Runtime Environment
- Platform Independence
Java Overviewβ
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle). Key characteristics:
- Platform Independent: "Write Once, Run Anywhere" (WORA)
- Object-Oriented: Everything is an object (except primitives)
- Strongly Typed: Variables must be declared with specific types
- Memory Managed: Automatic garbage collection
- Compiled and Interpreted: Bytecode compilation + JVM interpretation
Java Execution Processβ
1. Source Code (.java files)β
// Example: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Compilation Processβ
javac HelloWorld.java # Compiles to HelloWorld.class
What happens during compilation:
- Source code (.java) β Bytecode (.class)
- Syntax checking and error reporting
- Optimization at compile time
- Generation of platform-independent bytecode
3. Execution Processβ
java HelloWorld # Runs the bytecode
Execution flow:
- JVM loads the bytecode
- Bytecode verification
- Just-In-Time (JIT) compilation
- Native machine code execution
Java Virtual Machine (JVM)β
JVM Architectureβ
βββββββββββββββββββββββββββββββββββββββββββββββ
β JVM β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Class Loader Subsystem β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Memory Areas: β
β ββ Method Area β
β ββ Heap Memory β
β ββ Stack Memory β
β ββ PC (Program Counter) Register β
β ββ Native Method Stack β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Execution Engine: β
β ββ Interpreter β
β ββ JIT Compiler β
β ββ Garbage Collector β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Native Method Interface (JNI) β
βββββββββββββββββββββββββββββββββββββββββββββββ
JVM Componentsβ
1. Class Loader Subsystemβ
- Loading: Loads .class files into memory
- Linking: Verification, preparation, resolution
- Initialization: Static variables and blocks
2. Memory Areasβ
- Method Area: Class-level data, constants, static variables
- Heap: Object instances and arrays
- Stack: Method call frames, local variables
- PC Register: Current executing instruction pointer
- Native Method Stack: Native method calls
3. Execution Engineβ
- Interpreter: Executes bytecode line by line
- JIT Compiler: Compiles frequently used bytecode to native code
- Garbage Collector: Automatic memory management
Memory Managementβ
Heap Memory Structureβ
ββββββββββββββββββ βββββββββββββββββββββββββββββ
β HEAP MEMORY β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Young Generation: β
β ββ Eden Space (new objects) β
β ββ Survivor Space S0 β
β ββ Survivor Space S1 β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Old Generation (Tenured): β
β ββ Long-lived objects β
βββββββββββββββββββββββββββββββββββββββββββββββ€
β Metaspace (Java 8+): β
β β β Class metadata β
βββββββββββββββββββββββββββββββββββββββββββββββ
Stack Memoryβ
public class StackExample {
public static void main(String[] args) { // Frame 1
int x = 10;
methodA(x);
}
static void methodA(int a) { // Frame 2
int b = 20;
methodB(a, b);
}
static void methodB(int p, int q) { // Frame 3
int result = p + q;
System.out.println(result);
} // Frame 3 destroyed
} // Frame 2 destroyed, Frame 1 destroyed
Stack Frame Contains:
- Local variables
- Method parameters
- Return address
- Intermediate values
Memory Allocation Exampleβ
public class MemoryExample {
static int staticVar = 100; // Method Area
public static void main(String[] args) {
int localVar = 10; // Stack
String str = "Hello"; // String Pool (Heap)
Person p = new Person("John"); // Heap
// localVar, str reference β Stack
// "Hello", Person object β Heap
// staticVar β Method Area
}
}
Code Compilation & Executionβ
Compilation Stepsβ
- Lexical Analysis: Source code β Tokens
- Syntax Analysis: Tokens β Abstract Syntax Tree (AST)
- Semantic Analysis: Type checking, scope resolution
- Code Generation: AST β Bytecode
- Optimization: Bytecode optimization
Bytecode Exampleβ
// Java source
public int add(int a, int b) {
return a + b;
}
// Corresponding bytecode
public int add(int, int);
Code:
0: iload_1 // Load first parameter
1: iload_2 // Load second parameter
2: iadd // Add integers
3: ireturn // Return result
JIT Compilation Processβ
Bytecode β Profiling β Hot Spot Detection β Native Code Generation
JIT Optimization Levels:
- C1 Compiler: Fast compilation, basic optimizations
- C2 Compiler: Slower compilation, advanced optimizations
- Tiered Compilation: Combines C1 and C2
Java Runtime Environmentβ
JRE Componentsβ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Java Runtime Environment (JRE) β
βββββββββββββββββββββββββ ββββββββββββββββββββββ€
β ββ Java Virtual Machine (JVM) β
β ββ Core Libraries (java.lang, java.util) β
β ββ Supporting Files β
β ββ Configuration Files β
βββββββββββββββββββββββββββββββββββββββββββββββ
JDK vs JRE vs JVMβ
βββββββββββββββββββββββββββββββββββββββββββββββ
β Java Development Kit (JDK) β
β ββ Development Tools (javac, jar, etc.) β
β ββ Java Runtime Environment (JRE) β
β ββ Java Virtual Machine (JVM) β
β ββ Core Libraries β
βββββββββββββββββββββββββββββββββββββββββββββββ
Platform Independenceβ
How Java Achieves Platform Independenceβ
- Bytecode: Intermediate representation
- JVM: Platform-specific implementation
- Standard Libraries: Consistent APIs across platforms
βββββββββββββββ βββββββββββββββ βββββββββββββββ
βJava Source β β Bytecode β β Native β
β (.java) βββββΆβ (.class) βββββΆβ Code β
β β βPlatform β βPlatform β
βPlatform β βIndependent β βDependent β
βIndependent β β β β β
βββββββββββββββ ββββ βββββββββββ βββββββββββββββ
Memory Management Deep Diveβ
Garbage Collectionβ
Types of GC:
- Serial GC: Single-threaded
- Parallel GC: Multi-threaded
- CMS GC: Concurrent Mark Sweep
- G1 GC: Garbage First
- ZGC/Shenandoah: Low-latency collectors
GC Process Exampleβ
public class GCExample {
public static void main(String[] args) {
// Objects created in Eden space
Person p1 = new Person("Alice");
Person p2 = new Person("Bob");
p1 = null; // p1 becomes eligible for GC
// Force garbage collection (not recommended in production)
System.gc();
}
}
Performance Considerationsβ
JVM Tuning Parametersβ
# Heap size
java -Xms512m -Xmx2g MyApp
# Garbage Collector
java -XX:+UseG1GC MyApp
# JIT compilation
java -XX:+TieredCompilation MyApp
# Memory debugging
java -XX:+PrintGCDetails MyApp
Best Practicesβ
-
Memory Management:
- Avoid memory leaks
- Use appropriate data structures
- Close resources properly
-
Performance:
- Minimize object creation in loops
- Use StringBuilder for string concatenation
- Profile your application
-
JVM Configuration:
- Set appropriate heap sizes
- Choose suitable garbage collector
- Monitor JVM metrics
Summaryβ
Java's execution model provides platform independence through bytecode compilation and JVM interpretation. The JVM manages memory automatically through garbage collection and optimizes performance through JIT compilation. Understanding these concepts is crucial for writing efficient Java applications and troubleshooting performance issues.
Key takeaways:
- Java source code compiles to platform-independent bytecode
- JVM provides runtime environment with automatic memory management
- Memory is organized into heap, stack, and method areas
- JIT compilation optimizes frequently executed code
- Garbage collection automatically manages object lifecycle