yse / easy_profiler

Lightweight profiler library for c++
MIT License
2.14k stars 184 forks source link

Profiling blocks on short-lived threads 'leak' memory #178

Open mathieumallet opened 4 years ago

mathieumallet commented 4 years ago

(Please excuse incorrect terminology as I'm not completely familiar with the inner workings of easy_profiler)

When an EASY_BLOCK is encountered in code, the following are done:

  1. add a block descriptor for the block if one doesn't exist already
  2. register the current thread if it isn't already
  3. add a block of data to the current thread (if profiling is enabled)

However step 2 introduces an issue: if the EASY_BLOCK is encountered on a new thread, a new thread registration is done. This is fine (and wanted) when profiling, but when not profiling this results in threads being registered and kept in memory. If threads are constantly being created and destroyed, this results in increasing the memory usage over time.

This is what the allocation looks like in a profiler: image

The thread registration data is removed when the thread has expired, but only when a capture is completed. If profiling is not enabled, that data stays in memory until the app exits.

Two possible fixes for this:

We have a fix for this in our own fork of easy_profiler (as a new macro to 'end' a thread), but this may not meet the quality requirements for the main easy_profiler repo: https://github.com/YOU-i-Labs/easy_profiler/compare/v2.0.1-youi14...v2.0.1-youi14b