elastic / go-elasticsearch

The official Go client for Elasticsearch
https://github.com/elastic/go-elasticsearch#go-elasticsearch
Apache License 2.0
5.54k stars 609 forks source link

Fix ticker memory leak in bulk indexer due to internal flush call resetting the ticker #797

Closed tblyler closed 4 months ago

tblyler commented 4 months ago

Hello, I discovered that the time.Ticker instance within the BulkIndexer is consuming a bunch of memory in my long running application that creates and closes many instances of the BulkIndexer. See the following screenshots of a pprof heap visualization that I took of the application (forcing a garbage collection run too).

pprof

pprof flamegraph

It appears that the time.Ticker instance is Reset within the flush method, thus causing the memory leak. The fix that I have tested and confirmed working is to simply call Stop on the ticker after the flush call since it is valid to have that Reset call within the flush method for other parts of the BulkIndexer.

I would attach another screenshot of pprof, but the time.Ticker doesn't show up in the visualization due to its small memory footprint.