influxdb-ruby icon indicating copy to clipboard operation
influxdb-ruby copied to clipboard

InfluxDB::Client is never garbage collected

Open dgutov opened this issue 3 years ago • 5 comments
trafficstars

I'm guessing that happens (or doesn't happen) because of the at_exit block inside its constructor.

I think it should either be reworked to use just one global at_exit somewhere on the module level, or the docs should clarify somewhere that one should create one client instance globally. That is, to avoid creating instances dynamically per request (in a web app).

I also haven't managed to find any info as to whether the client is thread-safe, or we should use it through a connection pool.

dgutov avatar May 20 '22 15:05 dgutov

For some evidence:

require 'influxdb'
require 'memory_profiler'
MemoryProfiler.report { InfluxDB::Client.new }.pretty_print

=>

...
retained memory by class
-----------------------------------
       232  InfluxDB::Config
        80  Proc
        76  Thread::Queue
        40  Hash
        40  InfluxDB::Client

retained objects by gem
-----------------------------------
         3  influxdb-0.8.1
         2  other

retained objects by file
-----------------------------------
         2  (irb)
         2  /home/dgutov/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/influxdb-0.8.1/lib/influxdb/client.rb
         1  /home/dgutov/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/influxdb-0.8.1/lib/influxdb/config.rb

retained objects by location
-----------------------------------
         2  (irb):5
         1  /home/dgutov/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/influxdb-0.8.1/lib/influxdb/client.rb:54
         1  /home/dgutov/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/influxdb-0.8.1/lib/influxdb/client.rb:58
         1  /home/dgutov/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/influxdb-0.8.1/lib/influxdb/config.rb:130

retained objects by class
-----------------------------------
         1  Hash
         1  InfluxDB::Client
         1  InfluxDB::Config
         1  Proc
         1  Thread::Queue

dgutov avatar May 20 '22 15:05 dgutov

Thanks for reporting this issue. Have you looked into it further? I assume it is a hot issue and lies on the critical path.

hogaur avatar Dec 21 '22 19:12 hogaur

It wasn't that hot, but if the client is instantiated again and again, that did contribute to unbounded memory growth.

We ultimately went with either with a pool or with a global client instance. I can't remember which of these two, sorry.

dgutov avatar Dec 22 '22 18:12 dgutov

Hot or not, it doesn't matter if it doesn't occur now. Thanks.

hogaur avatar Dec 22 '22 18:12 hogaur

It does occur. Be careful not to instantiate a new instance of InfluxDb::Client in your web request handlers.

dgutov avatar Dec 22 '22 18:12 dgutov