Memory leak test: take fluctuations into account #1757
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Preamble
We have a memory leak test suite, which calls a function many times and fails if the process memory increased. We do this in order to detect missing
free()
orPy_DECREF
calls in the C modules. When we do, then we have a memory leak.The problem
A problem we've been having for probably over 10 years, is the false positives. That's because the memory fluctuates. Sometimes it may increase (or even decrease!) due to how the OS handles memory, the Python's garbage collector, the fact that RSS is an approximation and who knows what else. So thus far we tried to compensate that by using the following logic:
This logic didn't really solve the problem, as we still had occasional false positives, especially lately on FreeBSD.
The solution
This PR changes the internal algorithm so that in case of failure (mem > 0 after calling fun() N times) we retry the test for up to 5 times, increasing N (repetitions) each time, so we consider it a failure only if the memory keeps increasing between runs. So for instance, here's a legitimate failure:
If, on the other hand, the memory increased on one run (say 200 calls) but decreased on the next run (say 400 calls), then it clearly means it's a false positive, because memory consumption may be > 0 on second run, but if it's lower than the previous run with less repetitions, then it cannot possibly represent a leak (just a fluctuation):
Note about mallinfo()
Aka #1275.
mallinfo()
on Linux is supposed to provide memory metrics about how many bytes gets allocated on the heap bymalloc()
, so it's supposed to be way more precise than RSS and also USS. In another branch were I exposed it, I verified that fluctuations still occur even when usingmallinfo()
though, despite less often. So that means evenmallinfo()
would not grant 100% stability.