将Redis与生菜客户端连接时出现内存泄漏错误

时间:2018-12-17 09:51:34

标签: spring-boot redis lettuce

我正在使用生菜连接到Redis:spring-boot-starter-redis:2.0.6.RELEASE,当我启动应用程序时,它会出现此错误:

[ERROR][main][ResourceLeakDetector][error] - LEAK: 
HashedWheelTimer.release() was not called before it's garbage- 
collected. See http://netty.io/wiki/reference-counted-objects.html for 
more information.
Recent access records: 
Created at:
io.netty.util.HashedWheelTimer.<init>(HashedWheelTimer.java:272)
io.netty.util.HashedWheelTimer.<init>(HashedWheelTimer.java:216)
io.lettuce.core.resource.DefaultClientResources.<init>(DefaultClientResources.java:163)
io.lettuce.core.resource.DefaultClientResources$Builder.build(DefaultClientResources.java:461)
io.lettuce.core.resource.DefaultClientResources.create(DefaultClientResources.java:229)
io.lettuce.core.AbstractRedisClient.<init>(AbstractRedisClient.java:96)
io.lettuce.core.RedisClient.<init>(RedisClient.java:86)
io.lettuce.core.RedisClient.create(RedisClient.java:123)
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.lambda$createClient$7(LettuceConnectionFactory.java:853)
java.util.Optional.orElseGet(Optional.java:267)
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.createClient(LettuceConnectionFactory.java:853)
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.afterPropertiesSet(LettuceConnectionFactory.java:232)

redis配置文件:

  public class RedisConfig {

  @Bean
  RedisConnectionFactory lettuceConnectionFactory(RedisProperties 
   redisProperties) {
   // 
  }

  @Bean
  public RedisTemplate<String, Object> 
  redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {

  }

  private RedisStandaloneConfiguration connection(RedisProperties 
  redisProperties) {

  }
 }

config yaml文件:

  spring:      
   redis:
     host: 
     lettuce:
      pool:
       max-active:  100

有人帮忙吗?

2 个答案:

答案 0 :(得分:1)

当创建多个RedisClient实例而不共享ClientResources时,通常会发生这种类型的错误。

Spring Boot创建一个单例实例,因此任何其他实例都可能由您的代码或外部依赖项创建。没有更多的上下文,就不可能说更多。

答案 1 :(得分:0)

如果您在应用程序中使用许多RedisClient实例-您需要在它们之间共享ClientResources对象。

创建RedisClient的典型方法是:

    RedisClient.create("someUrl");

但是,当您要创建多个实例时,不建议使用这种对象创建方式。 在RedisClient内部,有一个带有重物的区域:ClientResources 在创建RedisClient的许多实例时,在运行时可能会产生错误

LEAK: You are creating too many HashedWheelTimer instances.  HashedWheelTimer is a shared resource that must be reused across the JVM,so that only a few instances are created.

您可以做的是在您的应用程序中创建一个共享的ClientResources。并将此对象传递给构造函数RedisClient

例如:

    ClientResources sharedResources = DefaultClientResources.create();

    //and use this between many RedisClients i.e.:

    RedisClient first = RedisClient.create(sharedResources, "someUrl");
    RedisClient second = RedisClient.create(sharedResources, "someUrl_222");
    RedisClient third = RedisClient.create(sharedResources, "someUrl_3333");

有关更多详情,请访问HERE