【问题标题】:Memory leak Error when connect redis with lettuce client将redis与生菜客户端连接时出现内存泄漏错误
【发布时间】:2018-12-17 09:51:34
【问题描述】:

我正在使用生菜连接到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) {

  }
 }

配置yaml文件:

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

有人帮忙吗?

【问题讨论】:

    标签: spring-boot redis lettuce


    【解决方案1】:

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

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

    【讨论】:

    • 嗨@mp911de,我已经用我的配置文件更新了帖子,请看一下
    • 如果您从代码中创建这样的 bean,我建议您在 LettuceConnectionConfiguration 运行之前创建 ClientResources bean 并将其添加到 spring 上下文并为您的用例使用相同的,或者您可以使用其他方式您只需自动装配由自动配置创建的 bean 并将其用于您创建的 bean。
    【解决方案2】:

    如果您在应用程序中使用许多 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

    【讨论】:

    • 生效了,太好了!
    猜你喜欢
    • 2019-05-15
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多