Java JVM 内存解析

举报
简简单单Onlinezuozuo 发表于 2022/02/19 00:52:42 2022/02/19
【摘要】 文章目录 Java JVM 内存解析1、最大可用内存 -Xmx2、虚拟机中可用内存量3、虚拟机总内存量4、虚拟机当前实际可用内存5、Log 方法 Java JVM...

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();

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
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();

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
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();

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
4、虚拟机当前实际可用内存
private static final long MB = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
// 实际可用 = 最大可用 - 总计内存 + 空余内存
( runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory()) / MB;

  
 
  • 1
  • 2
  • 3
  • 4

如何理解实际可用内存

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
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(" - - - - - - - - - - - - - - - - - - - - - - -");
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

文章来源: wretchant.blog.csdn.net,作者:简简单单OnlineZuozuo,版权归原作者所有,如需转载,请联系作者。

原文链接:wretchant.blog.csdn.net/article/details/84585087

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200