线上报错-jvm:堆外内存溢出OutOfMemoryError: direct buffer memory.md

参数配置

服务器内存
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… 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

转载于:https://juejin.im/post/5c51625df265da615c59a3ee

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值