1. 程式人生 > 實用技巧 >springboot整合redis報錯:OutOfDirectMemoryError

springboot整合redis報錯:OutOfDirectMemoryError

錯誤資訊

壓力測試時產生堆外記憶體溢位:OutOfDirectMemoryError

原因

  1. springboot 2.0 以後預設使用 lettuce 作為操作 redis 的客戶端,它使用 netty 進行通訊
  2. lettuce 的 bug 導致 netty 堆外記憶體溢位
  3. -Xmx300m:如果沒有指定堆外記憶體,netty 預設使用 堆記憶體(Xmx) 作為 堆外記憶體

pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

解決

方法一:

不能使用 -Dio.netty.maxDirectMemory 只調大堆外記憶體

  1. 升級 lettuce 客戶端
  2. 使用 Jedis(推薦)

pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>