rellocs wood
rellocs wood

Reputation: 1471

Memory leak Error when connect redis with lettuce client

I am using lettuce to connect to redis: spring-boot-starter-redis :2.0.6.RELEASE, when I startup the app it come out this error:

[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 config file :

  public class RedisConfig {

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

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

  }

  private RedisStandaloneConfiguration connection(RedisProperties 
  redisProperties) {

  }
 }

config yaml file :

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

is anyone help?

Upvotes: 2

Views: 10183

Answers (2)

sobczak.dev
sobczak.dev

Reputation: 1308

If you use many RedisClient instances in your application - you need to share between them ClientResources object.

The typical way to create RedisClient is:

    RedisClient.create("someUrl");

But this way of object creation is not recommended when you want create multiple instances. Inside RedisClient there is field with heavy object: ClientResources When you are creating many instances of RedisClient then error can be produced during runtime

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.

What you can do is to create one shared ClientResources in your application. And pass this object to the constructor RedisClient.

For example:

    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");

For more deatils, please visit HERE

Upvotes: 3

mp911de
mp911de

Reputation: 18119

This type of error typically occurs when multiple instances of RedisClient get created without sharing ClientResources.

Spring Boot creates a singleton instance so any other instances are likely to be created by either your code or external dependencies. Without further context, it's not possible to say more.

Upvotes: 1

Related Questions