Redis 报”OutOfDirectMemoryError“(堆外内存溢出)

Redis 报错“OutOfDirectMemoryError(堆外内存溢出) ”问题如下:

一、报错信息:

使用 Redis 的业务接口 ,产生 OutOfDirectMemoryError(堆外内存溢出),如图:

格式化后的报错信息:

{
	"timestamp": "2023-04-17 22:46:36",
	"status": 500,
	"error": "Internal Server Error",
	"message": "Java heap space",
	"trace": "java.lang.OutOfMemoryError: Java heap 
    ......
}

二、报错原因:

源码分析:

 public final class PlatformDependent {
     // 直接内存大小,可通过 “-Dio.netty.maxDirectMemory”参数设置。
     private static final long DIRECT_MEMORY_LIMIT;
     
     // 默认的最大直接内存大小 ,方法略。大概意思还是:先获取“Eclipse OpenJ9”的“sun.misc.VM”参数,如果没有则获取JVM的“-XX:MaxDirectMemorySize”作为默认值。
     private static final long MAX_DIRECT_MEMORY = maxDirectMemory0();
     
     static {
        // 其他赋值操作,略
         
      // 给 直接内存大小赋值,如果有设置 "-Dio.netty.maxDirectMemory" 参数,则使用用户设置的,如果没有则使用默认的
      logger.debug("-Dio.netty.maxDirectMemory: {} bytes", maxDirectMemory);
      DIRECT_MEMORY_LIMIT = maxDirectMemory >= 1 ? maxDirectMemory : MAX_DIRECT_MEMORY;
                
     }

    private static void incrementMemoryCounter(int capacity) {
        if (DIRECT_MEMORY_COUNTER != null) {
            long newUsedMemory = DIRECT_MEMORY_COUNTER.addAndGet(capacity);
            
            //关键判断:如果 netty内部使用的内存大小 大于 “直接内存大小”的话,就抛出 "OutOfDirectMemoryError"异常。
            if (newUsedMemory > DIRECT_MEMORY_LIMIT) {
                DIRECT_MEMORY_COUNTER.addAndGet(-capacity);
                throw new OutOfDirectMemoryError("failed to allocate " + capacity
                                                 + " byte(s) of direct memory (used: " + (newUsedMemory - capacity)
                                                 + ", max: " + DIRECT_MEMORY_LIMIT + ')');
            }
        }
    }
 }    

总结原因:

1)、Springboot 2.x 以后默认使用 Lettuce作为操作 redis 的客户端。它是使用 netty 进行网络通信的。

2)、从spring-boot-starter-data-redis(2.2.3.RELEASE) 依赖可以看出内置使用的确实是 Lettuce 客户端,分析源码得知,lettuce 使用的 netty 框架,引用的netty包netty-common-4.1.43.Final.jar里面有一个PlatformDependent.java类 ,底层有个-Dio.netty.maxDirectMemory 参数,会自己校验堆外内存是否大于当前服务可使用的内存,如果大于则抛出 OutOfDirectMemoryError(堆外内存溢出)。显然,这是属于 Netty(netty-common-4.1.43.Final.jar)的bug导致堆外内存溢出的。

三、解决方法:

不能使用-Dio.netty.maxDirectMemory 只调大堆外内存,这只能延迟bug出现的时机,不能完全解决该问题。想解决有如下两个方案:

1、升级 Lettuce客户端,期待新版本会解决该问题。

2、排除 Lettuce客户端,切换使用没有该问题的 Jedis 客户端。

Netty 框架性能更好,吞吐量更大,但是Springboot默认使用的Lettuce 客户端对Netty的支持不够好;

Jedis客户端虽然没有Netty更快,但胜在稳定,没有上述bug。

因此下面使用第二种解决方案:

切换使用Jedis 客户端:

在data-redis中排除lettuce,在引入jedis

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <!--排除 lettuce -->
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--引入 jedis -->
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

四、检验结果:

压力测试:

测试结果:

解决 Redis 报“OutOfDirectMemoryError(堆外内存溢出) 成功”

posted @ 2023-04-17 23:24  小謝同學  阅读(233)  评论(0编辑  收藏  举报