参数配置
服务器内存
1.堆最大 2G
2.操作系统 30G
3.堆外内存 //默认和堆内存一样大2G 目前线上服务器没有配置堆外内存参数
报错信息
堆外内存溢出OutOfMemoryError: direct buffer memory
代码分析
AppMessage服务 下载大文件 报错 而且是并发下载大文件
AppMessage包含以下功能 1.上传下载 2.推送消息 3.等等
原因分析
因为堆外内存类的底层使用步骤是:1.先fullGC 2.再分配堆外内存
所以 报错原因是 没有fullGC 导致分配报错
那么为什么没有fullGC?有没有配置禁止fullGC参数?也没有配置。
还有其他的原因吗?
源码分析
class Bits {
// These methods should be called whenever direct memory is allocated or
// freed. They allow the user to control the amount of direct memory
// which a process may access. All sizes are specified in bytes.
static void reserveMemory(long size, int cap) {
synchronized (Bits.class) {
if (!memoryLimitSet && VM.isBooted()) {
maxMemory = VM.maxDirectMemory();
memoryLimitSet = true;
}
// -XX:MaxDirectMemorySize limits the total capacity rather than the
// actual memory usage, which will differ when buffers are page
// aligned.
if (cap <= maxMemory - totalCapacity) {
reservedMemory += size;
totalCapacity += cap;
count++;
return;
}
}
System.gc(); //垃圾回收
try {
Thread.sleep(100); //睡眠 作用?因为刚刚的gc不会马上gc 只是申请 jvm空闲(活跃线程少)的时候才会真正的gc 所以睡眠一会儿 等待gc回收
} catch (InterruptedException x) {
// Restore interrupt status
Thread.currentThread().interrupt();
}
synchronized (Bits.class) {
if (totalCapacity + cap > maxMemory)
throw new OutOfMemoryError("Direct buffer memory");
reservedMemory += size;
totalCapacity += cap;
count++;
}
}
复制代码
System.gc()会触发一个full gc,当然前提是你没有显示的设置-XX:+DisableExplicitGC来禁用显式GC。并且你需要知道,调用System.gc()并不能够保证full gc马上就能被执行。 所以在后面打代码中,会进行最多9次尝试,看是否有足够的可用堆外内存来分配堆外内存。并且每次尝试之前,都对延迟等待时间,已给JVM足够的时间去完成full gc操作。如果9次尝试后依旧没有足够的可用堆外内存来分配本次堆外内存,则抛出OutOfMemoryError("Direct buffer memory”)异常。
作者:tomas家的小拨浪鼓 链接:www.jianshu.com/p/007052ee3… 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。