[filebeat] Critical memory leak of StandardMeter in broker.Close(), the element in the global arbiter map not released #42347
Labels
needs_team
Indicates that the issue/PR needs a Team:* label
https://github.com/elastic/beats/blob/v8.17.0/libbeat/outputs/kafka/config.go#L309-L315
If adding adapter.GoMetricsNilify in newSaramaConfig when creating a broker, the object of StandardMeter in arbiter map will not be released when calling broker.Close().
In Broker.Open(), registerMeter() will create a new StandardMeter, and save it to the global arbiter map (in go-metric lib).
But the adapter.GoMetricsNilify option changes the StandardMeter to NilMeter and save it to MetricRegistry, while the StandardMeter object has already created and saved in the global map arbiter. So the original StandardMeter object will not be deleted when calling MetricRegister.Unregister or UnregisterAll.
In our production environment, through mem profile, the inuse objects and allocate objects are the same which reaches highly to 1.4 million which takes nearly 400M space, resulting in OOM when the memory is limited.
Here is the benchmark to show the difference.
This code can add to here https://github.com/elastic/elastic-agent-libs/blob/v0.17.3/monitoring/adapter/go-metrics_test.go
The BenchmarkGoMetricsAdapterNilify's result shows that the inuse objects will remain the same with allocated objects.
But the BenchmarkGoMetricsAdapter's result shows that the inuse objects remain zero.
The text was updated successfully, but these errors were encountered: