[Zope] memory leak in a very simple product. Way?
Thomas B. Passin
tpassin@mitretek.org
Tue, 15 Jan 2002 14:36:15 -0500
[Christoph Wierling]
>
> I've writte a very simple product which creates 100 simple objects
> (testProd class instances) and each of these objects has a list-attribute
> which contains 200 other objects (item class instances). (Please find the
> code at the end of this mail.)
> By starting the addTestProd-function I create 20000 references to
> item objects, as the Zope-debug option as well as the leakFinder-product
> tell me.
> But I'll never get rid of these references and earlier or later I'll run
> out of memory.
>
> Could anybody explain me, way this simple product is leaking like a sieve?
> And how I can get rid of the references?
>
>
> here is the code:
>
> ...
> class testProd(SimpleItem.SimpleItem):
> """ testProd docu """
>
> meta_type = 'testProd'
>
> def __init__(self, id, title = ''):
> self.id = id
> self.title = title
>
> self._tree = []
>
> for i in xrange(200):
> n = item(str(i),i)
> tree = self._tree
> tree.append(n)
> self._tree = tree
>
I think you really want to do this:
tree = []
for i in xrange(200):
n = item(str(i),i)
tree.append(n)
self._tree = tree
I'm concerned about the statement
self._tree = tree
With your version, self._tree is assigned to tree, which is modified, then
assigned back to self._tree. Maybe that's OK, but it makes me concerned
that each time around a circular reference may be created. If so, there's
your leak.
Cheers,
Tom P