CFMTech / pytest-monitor

Pytest plugin for analyzing resource usage during test sessions
MIT License
173 stars 16 forks source link

Provide an option to force garbage collector to run between tests #40

Closed js-dieu closed 3 years ago

js-dieu commented 3 years ago

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

An option should be used to control this feature.

stas00 commented 3 years ago

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?

js-dieu commented 3 years ago

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.