在上一篇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大小。
---------------------
作者:开心的D哥
来源:CSDN
原文:https://blog.csdn.net/cloud_ll/article/details/50815671
版权声明:本文为博主原创文章,转载请附上博文链接!