JVM堆内存分配

堆内存配置

-Xms,设置程序启动时,初始堆大小
-Xms,设置程序启动时,最大堆大小
-XX:+PrintGC,打印GC日志信息
-XX:+UseSerialGC,配置串行回收器
-XX:+PrintGCDetails,查询各区详细信息
-XX:+PrintCommandLineFlags,输入详细参数配置

测试

package com.bjsxt.base001;

public class Test01 {

    public static void main(String[] args) {

        // -Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC
        // -XX:+PrintCommandLineFlags

        // 查看GC信息
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());

        byte[] b1 = new byte[1 * 1024 * 1024];
        System.out.println("分配了1M");
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());

        byte[] b2 = new byte[4 * 1024 * 1024];
        System.out.println("分配了4M");
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());

    }

}

配置方式
这里写图片描述
运行
输出信息
这里写图片描述
第一行,为打印配置参数

先分配了1M,空闲内存,减少了1M
又分配了4M,空闲控件不够,进行了一次GC
重新分配了可用空间

GC信息

[GC (Allocation Failure) [DefNew: 990K->128K(1152K), 0.0020815 secs][Tenured: 1592K->1719K(4864K), 0.0018109 secs] 2014K->1719K(6016K), [Metaspace: 2800K->2800K(1056768K)], 0.0043690 secs] [Times: user=0.00 sys=0.02, real=0.00 secs]

箭头左边,表示回收之前大小,990K
箭头右边,表示回收之后大小,128K

花费的时间
Times耗时
包括用户耗时、系统耗时、真实耗时
这里写图片描述
三个分别表示
新生代,包括伊甸园区、from区、to区
老年区
永久区

Total
总内存的大小

括号中的三个参数
第一个,表示内存的起始位置
第二个,表示内存的结束位置

Total=结束位置-起始位置
2304K =0x00000000fee70000- 0x00000000fec00000

猜你喜欢

转载自blog.csdn.net/nangeali/article/details/81784184