Cat导致内存不足原因分析

背景

线上几亿的数据在回刷的时候容器服务会出现OOM而重启,导致任务中断

内存泄露分析

jmap -histo pid 找出了有几十亿的java.lang.StackTraceElement对象,找不到被谁引用了
jmap -dump:format=b,file=heapdump.hprof pid dump内存
下载到本机mac上,用mat(MemoryAnalyzer)分析,得到内存泄露报告,看到内存全部被com.dianping.cat.message.internal.DefaultMessageManager$Context引用,找到了罪魁祸首
在这里插入图片描述

原因分析

com.dianping.cat.log4j.Log4j2Appender 在打印错误Exception的时候会调用Cat.logError方法

public class Log4j2Appender extends AbstractAppender {
  ....
    public void append(LogEvent event) {
        try {
            Level level = event.getLevel();
            if (level.isMoreSpecificThan(Level.WARN)) {
                this.logError(event);
            }
        } catch (Exception var3) {
            if (!this.ignoreExceptions()) {
                throw new AppenderLoggingException(var3);
            }
        }
    }
   ....
    private void logError(LogEvent event) {
        Throwable exception = event.getThrown();
        if (exception != null) {
            Message message = event.getMessage();
            if (message != null) {
                Cat.logError(message.getFormattedMessage(), exception);
            } else {
                Cat.logError(exception);
            }
        }

    }
}

Cat类的logError函数最终调用到了DefaultMessageManager.shouldLog方法

public class Cat {
    ...
    public static void logError(String message, Throwable cause) {
        try {
            getProducer().logError(message, cause);
        } catch (Exception var3) {
            errorHandler(var3);
        }

    }
    ...
    public static void logError(Throwable cause) {
        try {
            getProducer().logError(cause);
        } catch (Exception var2) {
            errorHandler(var2);
        }
    }
 }
public class DefaultMessageProducer implements MessageProducer {
    public void logError(String message, Throwable cause) {
        if (Cat.getManager().isCatEnabled()) {
            if (this.shouldLog(cause)) {
               ....
            }
        } else {
            cause.printStackTrace();
        }
    }
        private boolean shouldLog(Throwable e) {
        return this.m_manager instanceof DefaultMessageManager ? ((DefaultMessageManager)this.m_manager).shouldLog(e) : true;
    }

DefaultMessageManager类的m_context在shouldLog的时候把异常堆栈保存下来了,如果Cat事务不关闭,随着异常越来越多就导致了内存溢出

public class DefaultMessageManager {
    private ThreadLocal<DefaultMessageManager.Context> m_context = new ThreadLocal();
    boolean shouldLog(Throwable e) {
        DefaultMessageManager.Context ctx = (DefaultMessageManager.Context)this.m_context.get();
        return ctx != null ? ctx.shouldLog(e) : true;
    }

	class Context {
        // 内存不足就是由于错误堆栈信息没有限制导致的
        private Set<Throwable> m_knownExceptions;

        public Context(String domain, String hostName, String ipAddress) {
            ....
            this.m_knownExceptions = new HashSet();
        }

        public boolean shouldLog(Throwable e) {
            if (this.m_knownExceptions == null) {
                this.m_knownExceptions = new HashSet();
            }

            if (this.m_knownExceptions.contains(e)) {
                return false;
            } else {
                // 这里没有限制大小,只要有异常就往Set里面添加,这里应该做一个优化
                this.m_knownExceptions.add(e);
                return true;
            }
        }
   }
}

解决方案

手动调用Cat.getManager().reset();方法清空保存的异常堆栈信息

    public void reset() {
        DefaultMessageManager.Context ctx = (DefaultMessageManager.Context)this.m_context.get();
        if (ctx != null) {
            if (ctx.m_totalDurationInMicros == 0L) {
                ctx.m_stack.clear();
                ctx.m_knownExceptions.clear();
                this.m_context.remove();
            } else {
                // 这里会释放错误日志堆栈信息
                ctx.m_knownExceptions.clear();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值