[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [IMP-dev] memory leakage in IMP



Friedrich Foerster wrote:
> somehow i keep on stealing my computer memory when running imp from
> python. either my awkward programming style or a bug in imp/swig
> steals the memory from my system.

It is the former. ;) While Daniel is correct that you probably shouldn't
be calling those __del__ methods all over the place (to delete an object
'x', say 'del x', not 'x.__del__()') it isn't going to break anything.
The only place you need to call __del__ is in your assembly.__del__
method (where you correctly say b.__del__(self) to delete anything held
by the base classes). But because of Python's reference counting, you
don't need to delete things held by the classes (e.g. both
AssemblyRestraints.__del__ and assembly.__del__ are probably
unnecessary). You also don't need to explicitly call xxx.__del__() since
 that should happen automatically when xxx goes out of scope.

It is also true that you have to be a little bit careful when deriving
from IMP classes in Python, because C++ references do not map to
equivalent Python references. But this isn't an issue with IMP.Model,
since the lifetime of the C++ object is the same as the Python one.

Anyway, your problem is that assembly is never deleted because you have
a circular reference. It is a pure Python problem - nothing to do with
IMP. assembly.restraints is a reference to an AssemblyRestraints object.
 But AssemblyRestraints.assembs is a reference back to the assembly
object. Python can't delete 'assembly' because there's a reference to it
from AssemblyRestraints, but it can't delete AssemblyRestraints because
assembly has a reference to it... and so on. To delete the object, you
need to rethink your design so you don't have a circular reference or
(and this is a hack) you need to explicitly break the cycle by saying
something like 'del xxx.restraints'.

	Ben
-- 
                      http://salilab.org/~ben/
"It is a capital mistake to theorize before one has data."
	- Sir Arthur Conan Doyle