Java 25 - Learning Methods
- The Lifecycle of a Java Class: Compilation (
javac), Bytecode, Classloading. - Deep Dive: Bytecode: Reading
.classfiles withjavap -c -v -p module1.SimpleLifecycle. - Memory Management: Stack vs. Heap, Object Layout, Garbage Collection basics.
-
- Heap dump:
jmap -histo:live <PID> | head -n 10
- Heap dump:
-
- Stack Frame dump:
jstat -gcutil <PID> 500
- Stack Frame dump:
-
- Live Memory dump:
jstat -gc <PID> 500
- Live Memory dump:
- Garbage Collection: Types of GC (Serial, Parallel, CMS, G1), Tuning.
-
- Serial GC:
java -XX:+UseSerialGC- Low Overhead - Best Use Case:Tiny scripts, Lambda functions
- Serial GC:
-
- Parallel GC:
java -Xmx200m -XX:+UseParallelGC -Xlog:gc:stdout module1.MemoryDemo- Max Throughput │ Number crunching, ETL jobs.
- Parallel GC:
-
- CMS GC:
java -XX:+UseConcMarkSweepGC -Xmx200m -Xlog:gc:stdout module1.MemoryDemo- Balanced │ Web servers, standard apps (Default).
- CMS GC:
-
- G1 GC:
java -XX:+UseG1GC -Xmx200m -Xlog:gc:stdout module1.MemoryDemo- Min Latency │ Real-time apps, massive datasets.
- G1 GC:
- Execution Engine: Interpreter vs. JIT Compiler (C1/C2), Native Code.
-
- JIT Compilation:
java -XX:+PrintCompilation module1.JITDemo | grep "performCalculation"
- JIT Compilation:
-
-
- Compare Tiers:
java -XX:TieredStopAtLevel=1 module1.JITDemovsjava -XX:TieredStopAtLevel=4 module1.JITDemo
- Compare Tiers:
-
-
- Native Code:
javac -h module1.JITDemo
- Native Code:
- Data Modeling: Records, Sealed Classes/Interfaces.
-
javap -p module2.User
-
javap -v module2.TransactionStatus
-
javap -v module2.TransactionStatus | grep "Permitted" -A 3
- Control Flow: Pattern Matching for
switch, Record Patterns. - Text & Formatting: String Templates (if available/preview), Text Blocks.
- Virtual Threads: The M:N threading model, lightweight threads.
- Structured Concurrency: Managing thread lifecycles safely.
-
javac module3.StandardConcurrency.java
-
java module3.StandardConcurrency
-
StructuredTaskScopeis in preview, so we need to use--enable-previewflag.
- Scoped Values: Efficient alternatives to ThreadLocal.
-
javac --release 25 --enable-preview module3/ScopedValuesDemo.java
-
java --enable-preview module3.ScopedValuesDemo
- Foreign Function & Memory API: Calling C code without JNI.
-
javac --release 25 --enable-preview module4/PanamaHello.java
-
java --enable-native-access=ALL-UNNAMED module4.PanamaHello
- Vector API: SIMD (Single Instruction, Multiple Data) programming in Java.
-
javac --add-modules jdk.incubator.vector module4/VectorDemo.java
-
java --add-modules jdk.incubator.vector module4.VectorDemo
- JVM Flags: Tuning heap, GC algorithms (ZGC, G1).
- CDS (Class Data Sharing): Fast startup.
-
javac module5/HelloWorld.java
-
time java module5.HelloWorld
-
java -Xshare:dump -XX:SharedArchiveFile=app.jsa -cp . module5.HelloWorld
-
time java -Xshare:on -XX:SharedArchiveFile=app.jsa -cp . module5.HelloWorld
- Risk Analysis Server: Virtual Threads (Concurrency) + Vector API (Throughput).
-
javac --add-modules jdk.incubator.vector module6/RiskAnalysisServer.java
-
java --add-modules jdk.incubator.vector module6.RiskAnalysisServer
Focuses on Stability in long-running systems.
- Key Metrics: Latency, Throughput, Memory Footprint.
- Demo:
LongRunningGCStability.javasimulates a server under load to visualize GC impact.