This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Significant memory leak with PyImport_ReloadModule
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: bheld, collinwinter, georg.brandl, loewis, nnorwitz
Priority: normal Keywords:

Created on 2005-08-11 12:49 by bheld, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg26015 - (view) Author: Ben Held (bheld) Date: 2005-08-11 12:49
Having recently upgraded to Python 2.4, I am having a 
large memory 
leak with the following code built with VC++ 6.0: 


                PyObject *pName, *pModule; 


                Py_Initialize(); 
                pName = PyString_FromString(argv[1]); 


                pModule = PyImport_Import(pName); 
                Py_DECREF(pName); 


                PyObject* pModule2 = 
PyImport_ReloadModule(pModule)­; 
                Py_DECREF(pModule2); 
                Py_DECREF(pModule); 
                Py_Finalize(); 
                return 0; 


I get leaks of over 500 kb.  I have another program which 
is much more 
complex, in which every call to PyImport_ReloadModule 
is leaking 200+ 
kb, even though I am calling Py_DECREF correctly.
msg26016 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2005-08-13 13:34
Logged In: YES 
user_id=21627

How do you know there is a memory leak?
msg26017 - (view) Author: Ben Held (bheld) Date: 2005-08-16 13:56
Logged In: YES 
user_id=1327580

Boundschecker shows the leak and I have verified this by 
watching the process memory increase via the task manager.
msg26018 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2005-09-14 17:53
Logged In: YES 
user_id=1344176

I've been unable to verify this on Linux. I've tested python
versions 2.2.3, 2.3.5 and 2.4.1, all compiled with gcc 3.3.5
on Debian 3.1 under kernel 2.6.8. I used the sample program
provided by Ben, modified with an infinite loop over the
PyImport_ReloadModule/PyDECREF(pModule2) lines, sleeping for
1 second after every 25 iterations. I tested reloading the
modules distutils, os.path, distutils.command.sdist for 300+
iterations each under each python version. No memory leak
was observed.
msg26019 - (view) Author: Ben Held (bheld) Date: 2005-09-14 18:09
Logged In: YES 
user_id=1327580

This behavior is evident with Python 2.3.5 built on Windows 
with VC++ 6.0.
msg26020 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-09-20 06:02
Logged In: YES 
user_id=33168

I just tested with Python 2.4.2 on Linux with valgrind.  It
doesn't report any leaks.  So this could be specific to
Python 2.3.5, Windows, VC++ 6.0 or some other variation.  

Can you provide more info?  For example, what module are you
passing on the command line?  Can you provide that code? 
What objects specifically do you think are leaking?  Where
were they allocated?
msg26021 - (view) Author: Ben Held (bheld) Date: 2005-11-11 16:13
Logged In: YES 
user_id=1327580

Turns out this is not a Python issue, but a SWIG issue.  It 
only leaks for modules that were created with SWIG.  Please 
close this item
msg26022 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-11-11 19:46
Logged In: YES 
user_id=1188172

Closing as requested.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42267
2005-08-11 12:49:35bheldcreate