You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PyMem_RawMalloc(): update also sys.getallocatedblocks() in debug mode #71037
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2016-05-20.09:46:46.940>created_at=<Date2016-04-26.11:35:23.464>labels= ['type-feature']
title='PyMem_RawMalloc(): update also sys.getallocatedblocks() in debug mode'updated_at=<Date2016-05-20.09:46:46.939>user='https://github.com/vstinner'
I modified PyMem_Malloc() to use the pymalloc allocator in the issue bpo-26249. This change helped to find a memory leak in test_format that I introduced in Python 3.6: http://bugs.python.org/issue26249#msg264174
This memory leak gave me an idea: PyMem_RawMalloc() should also update sys.getallocatedblocks() (number of currently allocated blocks). It would help to find memory leaks using "python -m test -R 3:3" in extension modules using PyMem_RawMalloc() (and not PyMem_Malloc() or PyObject_Malloc()).
Attached patch uses an atomic variable _Py_AllocatedBlocks, but only in debug mode. I chose to only change the variable in debug mode to:
not impact performances
I don't know if atomic variables are well supported (especially the "var++" operation)
I don't know yet the impact of this change (how sys.getallocatedblocks() is used).
(The patch would be simpler if the release mode would also be impacted.)
The patch changes _PyObject_Alloc() and _PyObject_Free() in debug mode to only account allocations directly made by pymalloc, to let PyMem_RawMalloc() and PyMem_RawFree() update the _Py_AllocatedBlocks variable. In release mode, _PyObject_Alloc() and _PyObject_Free() are responsible to update the _Py_AllocatedBlocks variable for allocations delegated to PyMem_RawMalloc() and PyMem_RawFree().
Activity
vstinner commentedon Apr 26, 2016
I modified PyMem_Malloc() to use the pymalloc allocator in the issue bpo-26249. This change helped to find a memory leak in test_format that I introduced in Python 3.6:
http://bugs.python.org/issue26249#msg264174
This memory leak gave me an idea: PyMem_RawMalloc() should also update sys.getallocatedblocks() (number of currently allocated blocks). It would help to find memory leaks using "python -m test -R 3:3" in extension modules using PyMem_RawMalloc() (and not PyMem_Malloc() or PyObject_Malloc()).
Attached patch uses an atomic variable _Py_AllocatedBlocks, but only in debug mode. I chose to only change the variable in debug mode to:
(The patch would be simpler if the release mode would also be impacted.)
The patch changes _PyObject_Alloc() and _PyObject_Free() in debug mode to only account allocations directly made by pymalloc, to let PyMem_RawMalloc() and PyMem_RawFree() update the _Py_AllocatedBlocks variable. In release mode, _PyObject_Alloc() and _PyObject_Free() are responsible to update the _Py_AllocatedBlocks variable for allocations delegated to PyMem_RawMalloc() and PyMem_RawFree().
vstinner commentedon Apr 26, 2016
The expected side effect of this change is that hunting memory leaks in regrtest (python -m test) may get more noise.
I ran the Python test suite using "./python -m test -R 3:3": test_nntplib, test_tools and test_unittest failed.
Run alone, test_tools still fails:
-------------
But if I run test_tools one more time, it doesn't fail anymore...
Example of two sequential runs using two processes:
-------------
test_nntplib doesn't fail anymore when run alone.
Oh, and test_unittest failure doesn't seem related to my change: "./python -m test -R 3:3 test_unittest" already fails without my change.
vstinner commentedon May 20, 2016
I'm not convinced myself by this change, it may make detection of memory leaks much more tricky for a little gain. I prefer to reject my own issue ;-)