public class JVMOptionsExample {
public static void main(String[] args) {
// -Xms 设置JVM的初始堆内存大小
// 例如:-Xms512m 表示初始堆内存为512MB
// -Xmx 设置JVM的最大堆内存大小
// 例如:-Xmx1024m 表示最大堆内存为1024MB
// 示例代码,用于演示如何查看当前JVM的堆内存使用情况
Runtime runtime = Runtime.getRuntime();
System.out.println("Total Memory: " + runtime.totalMemory() / 1024 / 1024 + " MB");
System.out.println("Max Memory: " + runtime.maxMemory() / 1024 / 1024 + " MB");
System.out.println("Free Memory: " + runtime.freeMemory() / 1024 / 1024 + " MB");
// 模拟内存使用
byte[] memoryEater = new byte[64 * 1024 * 1024]; // 分配64MB内存
System.out.println("Allocated 64MB of memory.");
System.out.println("Free Memory after allocation: " + runtime.freeMemory() / 1024 / 1024 + " MB");
}
}
-Xms
是Java虚拟机(JVM)的一个启动参数,用于设置JVM的初始堆内存大小。例如,-Xms512m
表示JVM启动时分配512MB的堆内存。-Xmx
是另一个JVM启动参数,用于设置JVM的最大堆内存大小。例如,-Xmx1024m
表示JVM的最大堆内存为1024MB。Runtime
类来获取当前JVM的堆内存使用情况,并模拟了内存分配以展示内存变化。如果你运行这个程序并设置了不同的 -Xms
和 -Xmx
参数,你会看到不同的内存输出结果。
下一篇:java timestamp类型
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站