473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Unimporting modules, memory leak?

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 attached 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/pyt...ay/002669.html
[2] http://ax5.com/antonio/orangesnns

PD. If I attach the files the message does not reach to the list :-?
Code follows:

bigModule.py--------------------------------

def funcion_de_prueba(a,b):
print a,b

big_list = []
for i in xrange(1000):
big_list.append(1000 * 'biiiig')
--------------------------------------------

setupmod1.py--------------------------------

from distutils.core import setup, Extension

module1 = Extension('mod1',
sources = ['srcmod1.c'])

setup (name = 'OrangeSNNStmp',
version = '1.0',
description = 'Function that calls a trained NN',
ext_modules = [module1])
--------------------------------------------
srcmod1.c-----------------------------------

#include <Python.h>

static PyMethodDef orangeSnnsTmpMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC
initmod1(void)
{
(void) Py_InitModule("mod1", orangeSnnsTmpMethods);
}
--------------------------------------------
testMemory.py-------------------------------

#
# Test memory comsumption importing and unimporting modules
#
import gc, os, sys

def printMemoryUse():
rgc = gc.collect()
f = open("/proc/" + str(os.getpid()) + "/statm")
totalMemorySize = f.readline().split()[0]
f.close()
print "Mem. used:", totalMemorySize, "(gc:", rgc, ")"

def testImport(moduleName):
print "-- Testing", moduleName, "--"
printMemoryUse()

module = __import__(moduleName, globals(), locals())
printMemoryUse()

del sys.modules[moduleName]
print "Check refs (should be = 2):", sys.getrefcount(module)
del module
printMemoryUse()
testImport("mod1")
testImport("bigModule")
testImport("random")
--------------------------------------------

--
Regards,
Antonio Arauzo Azofra
Jun 14 '06 #1
0 1406

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
0
by: Antonio Arauzo Azofra | last post by:
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 ). In fact, it is...
3
by: crazy420fingers | last post by:
I'm running a python program that simulates a wireless network protocol for a certain number of "frames" (measure of time). I've observed the following: 1. The memory consumption of the program...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.