实战JVM参数配置
2019-03-01 本文已影响0人
127efd260e6e
本案例是想通过JVM参数配置了解到Java虚拟机内存默认参数的设置
代码如下
package com.bjsxt.base001;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
public class Test01 {
public static void main(String[] args) {
//-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags
DecimalFormat df = new DecimalFormat("#,###");
//查看GC信息
// System.out.println("max memory:" + df.format(Runtime.getRuntime().maxMemory()));
// System.out.println("free memory:" + df.format(Runtime.getRuntime().freeMemory()));
// System.out.println("total memory:" + df.format(Runtime.getRuntime().totalMemory()));
//
// byte[] b1 = new byte[1*1024*1024];
// System.out.println("分配了1M");
// System.out.println("max memory:" + df.format(Runtime.getRuntime().maxMemory()));
// System.out.println("free memory:" + df.format(Runtime.getRuntime().freeMemory()));
// System.out.println("total memory:" + df.format(Runtime.getRuntime().totalMemory()));
Map<Integer,Object> map=new HashMap();
for (int i = 0; i < 10; i++) {
byte[] b2 = new byte[1*1024*1024];
System.out.println("max memory:" + df.format(Runtime.getRuntime().maxMemory()));
System.out.println("free memory:" + df.format(Runtime.getRuntime().freeMemory()));
System.out.println("total memory:" + df.format(Runtime.getRuntime().totalMemory()));
// map.put(i, b2);
}
System.out.println("分配了4M");
System.out.println("max memory:" + df.format(Runtime.getRuntime().maxMemory()));
System.out.println("free memory:" + df.format(Runtime.getRuntime().freeMemory()));
System.out.println("total memory:" + df.format(Runtime.getRuntime().totalMemory()));
}
}
测试过程
启动时配置jvm参数
-Xms30m -Xmx30m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags
运行发现会进行一次[GC (Allocation Failure),然后最后分配如下
Heap
def new generation total 9216K, used 5031K [0x00000000fe200000, 0x00000000fec00000, 0x00000000fec00000)
eden space 8192K, 52% used [0x00000000fe200000, 0x00000000fe63c3a8, 0x00000000fea00000)
from space 1024K, 67% used [0x00000000feb00000, 0x00000000febada58, 0x00000000fec00000)
to space 1024K, 0% used [0x00000000fea00000, 0x00000000fea00000, 0x00000000feb00000)
tenured generation total 20480K, used 0K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
the space 20480K, 0% used [0x00000000fec00000, 0x00000000fec00000, 0x00000000fec00200, 0x0000000100000000)
Metaspace used 3387K, capacity 4560K, committed 4864K, reserved 1056768K
class space used 372K, capacity 388K, committed 512K, reserved 1048576K
很显然只会在年轻代进行处理
接下来我们修改jvm参数
-Xms3m -Xmx3m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags
发现每次循环都会进行一次[GC (Allocation Failure),然后最后分配如下
Heap
def new generation total 1216K, used 1070K [0x00000000ffc00000, 0x00000000ffd50000, 0x00000000ffd50000)
eden space 1088K, 98% used [0x00000000ffc00000, 0x00000000ffd0ae00, 0x00000000ffd10000)
from space 128K, 2% used [0x00000000ffd10000, 0x00000000ffd10d48, 0x00000000ffd30000)
to space 128K, 0% used [0x00000000ffd30000, 0x00000000ffd30000, 0x00000000ffd50000)
tenured generation total 2752K, used 691K [0x00000000ffd50000, 0x0000000100000000, 0x0000000100000000)
the space 2752K, 25% used [0x00000000ffd50000, 0x00000000ffdfcd50, 0x00000000ffdfce00, 0x0000000100000000)
Metaspace used 3386K, capacity 4560K, committed 4864K, reserved 1056768K
class space used 372K, capacity 388K, committed 512K, reserved 1048576K
测试了几次,基本都是上面的结果
总结
虚拟机内存分配:年轻代1/3,老年代2/3.
Eden区8/10,from区1/10,to区1/10
但是每次第二次试验老年代基本都为691k很奇怪,记录一下