<feed xmlns='http://www.w3.org/2005/Atom'>
<title>delta/python-packages/psutil.git/docs/conf.py, branch root-fs-device</title>
<subtitle>github.com: giampaolo/psutil.git
</subtitle>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/'/>
<entry>
<title>Memory leak test: take fluctuations into account (#1757)</title>
<updated>2020-05-12T22:40:04+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2020-05-12T22:40:04+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=6adcca6c9fda5a36e923bfca3591b972e98a0ce5'/>
<id>6adcca6c9fda5a36e923bfca3591b972e98a0ce5</id>
<content type='text'>
Preamble
=======

We have a [memory leak test suite](https://github.com/giampaolo/psutil/blob/e1ea2bccf8aea404dca0f79398f36f37217c45f6/psutil/tests/__init__.py#L897), which calls a function many times and fails if the process memory increased. We do this in order to detect missing `free()` or `Py_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:
- warmup (call fun 10 times)
- call the function many times (1000)
- if memory increased before/after calling function 1000 times, then keep calling it for another 3 secs
- if it still increased at all (&gt; 0) then fail

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 &gt; 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:

```
psutil.tests.test_memory_leaks.TestModuleFunctionsLeaks.test_disk_partitions ... 
Run #1: extra-mem=696.0K, per-call=3.5K, calls=200
Run #2: extra-mem=1.4M, per-call=3.5K, calls=400
Run #3: extra-mem=2.1M, per-call=3.5K, calls=600
Run #4: extra-mem=2.7M, per-call=3.5K, calls=800
Run #5: extra-mem=3.4M, per-call=3.5K, calls=1000
FAIL
```

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 &gt; 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):

```
psutil.tests.test_memory_leaks.TestModuleFunctionsLeaks.test_net_connections ... 
Run #1: extra-mem=568.0K, per-call=2.8K, calls=200
Run #2: extra-mem=24.0K, per-call=61.4B, calls=400
OK
```

Note about mallinfo()
================

Aka #1275. `mallinfo()` on Linux is supposed to provide memory metrics about how many bytes gets allocated on the heap by `malloc()`, so it's supposed to be way more precise than RSS and also [USS](http://grodola.blogspot.com/2016/02/psutil-4-real-process-memory-and-environ.html). In another branch were I exposed it, I verified that fluctuations still occur even when using `mallinfo()` though, despite less often. So that means even `mallinfo()` would not grant 100% stability.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Preamble
=======

We have a [memory leak test suite](https://github.com/giampaolo/psutil/blob/e1ea2bccf8aea404dca0f79398f36f37217c45f6/psutil/tests/__init__.py#L897), which calls a function many times and fails if the process memory increased. We do this in order to detect missing `free()` or `Py_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:
- warmup (call fun 10 times)
- call the function many times (1000)
- if memory increased before/after calling function 1000 times, then keep calling it for another 3 secs
- if it still increased at all (&gt; 0) then fail

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 &gt; 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:

```
psutil.tests.test_memory_leaks.TestModuleFunctionsLeaks.test_disk_partitions ... 
Run #1: extra-mem=696.0K, per-call=3.5K, calls=200
Run #2: extra-mem=1.4M, per-call=3.5K, calls=400
Run #3: extra-mem=2.1M, per-call=3.5K, calls=600
Run #4: extra-mem=2.7M, per-call=3.5K, calls=800
Run #5: extra-mem=3.4M, per-call=3.5K, calls=1000
FAIL
```

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 &gt; 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):

```
psutil.tests.test_memory_leaks.TestModuleFunctionsLeaks.test_net_connections ... 
Run #1: extra-mem=568.0K, per-call=2.8K, calls=200
Run #2: extra-mem=24.0K, per-call=61.4B, calls=400
OK
```

Note about mallinfo()
================

Aka #1275. `mallinfo()` on Linux is supposed to provide memory metrics about how many bytes gets allocated on the heap by `malloc()`, so it's supposed to be way more precise than RSS and also [USS](http://grodola.blogspot.com/2016/02/psutil-4-real-process-memory-and-environ.html). In another branch were I exposed it, I verified that fluctuations still occur even when using `mallinfo()` though, despite less often. So that means even `mallinfo()` would not grant 100% stability.</pre>
</div>
</content>
</entry>
<entry>
<title>update doc</title>
<updated>2019-04-05T10:20:52+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2019-04-05T10:20:52+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=9d2f9bf98f1d8d3a23228931ef13792c2d3bc007'/>
<id>9d2f9bf98f1d8d3a23228931ef13792c2d3bc007</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>migration towards RTD doc theme</title>
<updated>2017-09-07T08:02:13+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2017-09-07T08:02:13+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=274d6f63dbab0603d4cc714fa0ea4e5f7f5de883'/>
<id>274d6f63dbab0603d4cc714fa0ea4e5f7f5de883</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>update doc</title>
<updated>2016-12-19T00:29:27+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2016-12-19T00:29:27+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=e7d0efad3fa44de4310925cf3063132545edc3bd'/>
<id>e7d0efad3fa44de4310925cf3063132545edc3bd</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>GIT pre-commit script: exit if line ends with space</title>
<updated>2016-11-25T23:26:23+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2016-11-25T23:26:23+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=907a19e123bfe2a8bfdf1b69c16d73c61f27e7a3'/>
<id>907a19e123bfe2a8bfdf1b69c16d73c61f27e7a3</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>#521 remove python 2.4 and 2.5 support</title>
<updated>2014-11-01T17:07:31+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2014-11-01T17:07:31+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=02d58e96c321ed66ecccb3abb5430df2657c3fca'/>
<id>02d58e96c321ed66ecccb3abb5430df2657c3fca</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>minor doc and tox fixes</title>
<updated>2014-06-02T18:55:44+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2014-06-02T18:55:44+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=f7f2080fb96a065a7c2ae925f72086a5a4eeef13'/>
<id>f7f2080fb96a065a7c2ae925f72086a5a4eeef13</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>fix travis error on python 3.2; use flake8 in tox; make tox test also python 3.2</title>
<updated>2014-05-26T22:15:03+00:00</updated>
<author>
<name>Giampaolo Rodola</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2014-05-26T22:15:03+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=710fccca62d099574330f0c4d06ec2228cf6e8a1'/>
<id>710fccca62d099574330f0c4d06ec2228cf6e8a1</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>github migration</title>
<updated>2014-05-23T10:18:46+00:00</updated>
<author>
<name>Giampaolo Rodola'</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2014-05-23T10:18:46+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=a6ecb26350231a9da4bf456d37c79cf1a6d03eb8'/>
<id>a6ecb26350231a9da4bf456d37c79cf1a6d03eb8</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>provide doc favicon + add a 'make upload-doc' command which uploads doc on pythonhosted.org</title>
<updated>2014-04-08T21:51:49+00:00</updated>
<author>
<name>Giampaolo Rodola'</name>
<email>g.rodola@gmail.com</email>
</author>
<published>2014-04-08T21:51:49+00:00</published>
<link rel='alternate' type='text/html' href='http://git.baserock.org/cgit/delta/python-packages/psutil.git/commit/?id=c1987d049648549a8511f63ff7e0a185bd275c4c'/>
<id>c1987d049648549a8511f63ff7e0a185bd275c4c</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
</feed>
