Java Max Direct Memory Size设置

在上一篇Java Executor Service中我提到了我们的task因为OutofMemoryError: Direct Buffer Memory被Java Executor Service意外中止。接下来就该研究为什么系统会出现OOM Error。
一开始怀疑内存设置不够。那么我们就对DirectMemory设置进行了调查。
我们的系统中没有使用MaxDirectMemorySize参数设置DirectMemoryArea大小,那么我们肯定是使用了系统的默认配置了。JVM的默认配置是什么呢?我们来看看sun.misc.VM.java里面是如何设置默认的directmemorysize的

public static long maxDirectMemory() {  
    if (booted)  
        return directMemory;  

    Properties p = System.getProperties();  
    String s = (String)p.remove("sun.nio.MaxDirectMemorySize");  
    System.setProperties(p);  

    if (s != null) {  
        if (s.equals("-1")) {  
            // -XX:MaxDirectMemorySize not given, take default  
            directMemory = Runtime.getRuntime().maxMemory();  
        } else {  
            long l = Long.parseLong(s);  
            if (l > -1)  
                directMemory = l;  
        }  
    }  

    return directMemory;  
}  

最重要的就是以下这句:directMemory的大小由JVM运行时的最大内存来决定。

 directMemory = Runtime.getRuntime().maxMemory()

那么maxMemory()是用native来实现的,我们来看看java文档是如何说说它的返回。
*The maxMemory() method returns the maximum amount of memory that the VM
will attempt to use, allowing applications to better manage memory load.
If the implementation-dependent -Xmx flag is used then this method will
return that value.*
可见默认设置和Heap的Size差不多。在我们系统中使用了第三方lib,要求使用最少1G的Direct Memory Size。而我们系统中Xmx设置是512M。所以我们加入了MaxDirectMemorySize参数来指定DirectMemoryArea大小。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值