CFMTech/pytest-monitor

Provide an option to force garbage collector to run between tests

js-dieu opened this issue · 2 comments

The way the memory usage is collected does not take care of the garbage collector.
The idea here is to provide two modes:

  • The first one is the current one: garbage is managed by python during tests execution
  • the second one uses a stricter approach by enforcing the garbage collector to be run between two tests.

An option should be used to control this feature.

In my experience in testing environment it should be safe to call gc.collect() at will, without impacting user code base - because users can't control its automatic behavior anyway. So by calling it explicitly no expected behavior is modified.

My understanding is that the best time to run it is just before taking the any memory snapshot, because then you give a chance to potentially large variables to be reclaimed. Unless the call runs in a tight loop when for example you try to catch peak memory usage in a dedicated thread and then you can't afford gc.collect() since you may miss the peak then.

So perhaps the default can be to run gc.collect() at least before the last measurement. But perhaps to be consistent doing this before each measurement?

Hello @stas00

Did some progress on this ticket.
I manage to get the gc cleaned up on every test.
Running it before entering the test gives good results.
I'll make the PR tomorrow in the evening.