絮叨
本来打算是准备着一支烟 一杯咖啡 一个bug写一天的,但是一来公司,运维就把昨天生产线的日志怼过来说,线上报错了。。。
堆外内存报错
2020-03-24 03:35:39.486 ERROR --- [ForkJoinPool.commonPool-worker-0] com.hq.tiku.sync.bw.listener.ClassPlainListenter Line:101 - 同步消息队列push.userClassplan.tiku出现异常ClassPlanEntity(classplanId=2c7cc389822b4181a591c9890a2d3eeb, teacherId=276, teacherName=陈文文, teacherTeam=null, userId=18658702, className=2020年初级冲剌班, courseId=2900, courseNo=2020005CJ, courseName=2020年初级会计实务冲刺阶段, classplanName=2020年初级会计实务(冲刺阶段), classId=5864, tkUserId=null, courseTkNo=null, planNum=null) org.springframework.data.redis.RedisSystemException: Redis exception; nested exception is io.lettuce.core.RedisException: io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 855638016 byte(s) of direct memory (used: 985661440, max: 1823473664) at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:74) at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41) at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44) at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42) at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:257) at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.convertLettuceAccessException(LettuceStringCommands.java:718) at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.set(LettuceStringCommands.java:143) at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConnection.java:231) at org.springframework.data.redis.core.DefaultValueOperations$3.inRedis(DefaultValueOperations.java:202) at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:59) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184) at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95) at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:198) at com.hq.tiku.sync.bw.listener.ClassPlainListenter.lambda$synClassPlan$0(ClassPlainListenter.java:92) at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) at java.util.stream.Nodes$ArrayNode.forEach(Nodes.java:684) at java.util.stream.ForEachOps$ForEachOrderedTask.onCompletion(ForEachOps.java:490) at java.util.concurrent.CountedCompleter.tryComplete(CountedCompleter.java:577) at java.util.stream.ForEachOps$ForEachOrderedTask.onCompletion(ForEachOps.java:505) at java.util.concurrent.CountedCompleter.tryComplete(CountedCompleter.java:577) at java.util.stream.ForEachOps$ForEachOrderedTask.onCompletion(ForEachOps.java:505) at java.util.concurrent.CountedCompleter.tryComplete(CountedCompleter.java:577) at java.util.stream.ForEachOps$ForEachOrderedTask.doCompute(ForEachOps.java:483) at java.util.stream.ForEachOps$ForEachOrderedTask.compute(ForEachOps.java:400) at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) Caused by: io.lettuce.core.RedisException: io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 855638016 byte(s) of direct memory (used: 985661440, max: 1823473664) at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:125) at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:62) at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80) at com.sun.proxy.$Proxy227.set(Unknown Source) at org.springframework.data.redis.connection.lettuce.LettuceStringCommands.set(LettuceStringCommands.java:141) ... 23 common frames omitted 复制代码
上面是报错的信息,这个模块是一个同步模块,代码同事写的,就是把一些数据通过mq同步到数据库去,上面报错的就是因为堆外内存不够了,failed to allocate 855638016 byte(s) of direct memory (used: 985661440, max: 1823473664)
分析事故原因
首先我定位到代码处,把代码拿出来康康
在Rediss操作的时间报错了,然后网上各种百度,还真没找到具体的原因,不过自己定位了个大概。觉得有以下可能
- 第一就是因为它这个是循环set 然后用的redis spring data连的redis redis导致的,然后我想着用Pipeline批量拆入会不会好一点,把它改成批量插入。对netty不熟悉导致的
- 第二个就是确实是堆外内存不够了,默认情况下堆外内存是和堆内存的是一样的,但是我这个报错刚好是不够了,但是我只能暂时给他加大一点点,因为我不知道具体为啥会变大了这个还没排查出来,然后重启之后也没再出现这个错误了。。真的是玄学,唉d
- 或者是get的时候 从redis中拿得数据放到了直接内存中??
- 下次叫运维dump一下堆栈信息看看吧,这次没得搞了,已经重现不来了
- 可以通过 -XX:MaxDirectMemorySize调整大小,内存不足时抛出 OutOfMemoryError或者OutOfMemoryError:Direct buffer memory ,目前我已经让运维设置的大了一点了
- 后面我又想是不是因为这个服务一直在写数据,但是写的话虽然会创建很多对象,但是是在堆里面的,最多youngGC的时候就会把它清除了,并不会导致堆外内存溢出呀,百思不得其解
Redis客户端Lettuce源码【二】Lettuce是如何基于Netty建立连接的
结尾
其实也没啥,就是自己记录一下一些东西,哈哈。不过有大佬碰到的可以下面留言哦。