Java
Beginner
1 min read
Understanding the Java Compilation Model and the JVM
Example
// File: JvmDemo.java
// Demonstrates compilation units, packages, and basic JVM information
package com.example.intro; // maps to directory: com/example/intro/
public class JvmDemo {
public static void main(String[] args) {
// ---- Runtime / JVM info ----
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory() / (1024 * 1024); // convert to MB
long totalMemory = rt.totalMemory() / (1024 * 1024);
long freeMemory = rt.freeMemory() / (1024 * 1024);
int processors = rt.availableProcessors();
System.out.println("=== JVM Runtime Information ===");
System.out.printf("Max heap memory : %d MB%n", maxMemory);
System.out.printf("Total heap memory: %d MB%n", totalMemory);
System.out.printf("Free heap memory : %d MB%n", freeMemory);
System.out.printf("Available CPUs : %d%n", processors);
// ---- System properties ----
System.out.println("\n=== Key System Properties ===");
String[] keys = {"java.version", "java.vendor", "os.name", "user.dir"};
for (String key : keys) {
System.out.printf("%-20s = %s%n", key, System.getProperty(key));
}
}
}