Java JVM 内存解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15071263/article/details/84585087

Java JVM 内存解析


1、最大可用内存 -Xmx

设置虚拟机最大可以使用的内存总量

    // JDK 源码
    /**
     * Returns the maximum amount of memory that the Java virtual machine will
     * 返回虚拟机设置的    -Xmx 值的内存量,如果不设置,默认情况下是电脑内存的1/4,一般情况下
     * 会不够1/4,因为有一部分的物理内存要留给硬件,比如16G的内存,虚拟机默认内存就会在
     * 3600-3800M 左右
     *   
     * 如果没有找到该值,这个方法就会返回Long类型的最大值
     * attempt to use.  If there is no inherent limit then the value {@link
     * java.lang.Long#MAX_VALUE} will be returned.
     *
     * @return  the maximum amount of memory that the virtual machine will
     *          attempt to use, measured in bytes
     * @since 1.4
     */
    public native long maxMemory();
2、虚拟机中可用内存量

就是 JVM 当前空闲内存量 ,但是并不是最大内存减去已用内存就是可用内存

    // JDK 源码
    /**
     * Returns the amount of free memory in the Java Virtual Machine.
     * Calling the
     * <code>gc</code> method may result in increasing the value returned
     * by <code>freeMemory.</code>
     *  
     * 返回虚拟机中的可用内存量,当进行GC的时候,可能会导致该值增加
     *
     * @return  an approximation to the total amount of memory currently
     *          available for future allocated objects, measured in bytes.
     */
    public native long freeMemory();
3、虚拟机总内存量
    // JDK 源码
    /**
     * Returns the total amount of memory in the Java virtual machine.
     * The value returned by this method may vary over time, depending on
     * the host environment.
     * <p>
     * Note that the amount of memory required to hold an object of any
     * given type may be implementation-dependent.
     *  
     *  
     * 这个值会因为服务器主机的环境变化而变化,返回的是虚拟机当前已经使用了量加上已经占坑了
     * 但是却没有使用的内存总量,类似于Map 的扩容
     *
     * @return  the total amount of memory currently available for current
     *          and future objects, measured in bytes.
     */
    public native long totalMemory();
4、虚拟机当前实际可用内存
private static final long MB = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
// 实际可用 = 最大可用 - 总计内存 + 空余内存
( runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory()) / MB;

如何理解实际可用内存

假设虚拟机设置最大内存4096MB,4G
虚拟机的 totalMemory 是2G,也就是占坑2G
其中,虚拟机正在运行的时候使用了1.2G,那么 freeMemory 就是0.8G
虚拟机没用到的内存,一共是 2G,占坑了但是没有存放对象的是 0.8 G,
那么,实际剩余内存就是 4G - 2G + 0.8 G = 2.8G
5、Log 方法

    private static final long MB = 1024 * 1024;

    /**
     * -Xmx   Java Heap最大值,默认值为物理内存的1/4,最佳设值应该视物理内存大小及计算机内其他内存开销而定;
     *
     * -Xms   Java Heap初始值,Server端JVM最好将-Xms和-Xmx设为相同值,开发测试机JVM可以保留默认值;
     *
     * -Xmn   Java Heap Young区大小,不熟悉最好保留默认值;
     *
     * -Xss   每个线程的Stack大小,不熟悉最好保留默认值;
     *
     * 本方法用来监控 JVM 内存状态
     */
    public static void currentMemory() {
        Runtime runtime = Runtime.getRuntime();
        log.info(" - - - - - - - - - - - - - - - - - - - - - - -");
        log.info("JVM 最大可用内存 -Xmx [{}] MB", runtime.maxMemory()/MB);
        log.info("JVM 当前可用内存 [{}] MB", runtime.freeMemory()/MB);
        log.info("JVM 当前总计内存 [{}] MB", runtime.totalMemory()/MB);
        log.info("JVM 实际可用内存 [{}] MB",( runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory())/MB);
        log.info(" - - - - - - - - - - - - - - - - - - - - - - -");
    }

猜你喜欢

转载自blog.csdn.net/qq_15071263/article/details/84585087
今日推荐