Unimporting modules, memory leak?

Antonio Arauzo Azofra arauzo at uco.es
Mon Jun 12 13:19:29 EDT 2006


Hello everybody,

Probably, this is being too demanding for Python, but it may be
useful to unimport modules to work with dynamic code (though not the
best, one example is [2]). In fact, it is supposed to be possible[1],
but I have detected it usually leaks memory.

When unimported in Linux, the simple C module attached (has no
functions, just the structure) leaks two memory pages

To see the test you can just put the files in a directory and:
     python setupmod1.py install --install-lib .
     python testMemory.py

Its output follows. First, the memory used before import. Second memory
used after the import. Third the number of references to that object is
checked before using del. Finally the memory used after unimporting.

-- Testing mod1 --
Mem. used: 1242 (gc: 0 )
Mem. used: 1244 (gc: 0 )
Check refs (should be = 2): 2
Mem. used: 1244 (gc: 0 )
-- Testing bigModule --
Mem. used: 1244 (gc: 0 )
Mem. used: 2686 (gc: 0 )
Check refs (should be = 2): 2
Mem. used: 1244 (gc: 0 )
-- Testing random --
Mem. used: 1244 (gc: 0 )
Mem. used: 1256 (gc: 0 )
Check refs (should be = 2): 2
Mem. used: 1256 (gc: 57 )

Unimporting attached "bigmodule.py" there are no memory leaks.
Unimporting python's random module it leaks some pages, but garbage
collector admit it can not free them.

If a module with the same name that the unimported module is imported,
the pages are reused. While, if this same module is loaded with another
name, they are not freed, and the program grows with each module
imported/unimported

Is this a Python bug? A Linux bug? Am i missing some way of freeing that
memory used by C module?

[1] http://mail.python.org/pipermail/python-list/1999-May/002669.html
[2] http://ax5.com/antonio/orangesnns
-- 
All the best,
     Antonio Arauzo Azofra


-------------- next part --------------
A non-text attachment was scrubbed...
Name: bigModule.py
Type: text/x-python
Size: 127 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060612/d86a7cd5/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: setupmod1.py
Type: text/x-python
Size: 270 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060612/d86a7cd5/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: srcmod1.c
Type: text/x-csrc
Size: 224 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060612/d86a7cd5/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testMemory.py
Type: text/x-python
Size: 688 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20060612/d86a7cd5/attachment-0002.py>


More information about the Python-list mailing list