[Zope] Does this create a leak?

Maric MICHAUD maric at aristote.info
Tue May 18 08:15:48 EDT 2004

Jean-Francois.Doyon at CCRS.NRCan.gc.ca a écrit :
> Hello,
> Does setting a persistent object as attribute to another persistent
> object, and then accessing it directly through the web, create a leak?
> For example:
> from OFS.Image import Image
> class myclass(Image):
>     def __init__(self)
>         self.thumbnail = Image(id='thumbnail')
> and then access it using something like:
There's no /leak/, but the thumbnail attribute is a persistent attribute 
of instances of 'myclass'.

> http://host.com/myinstance/thumbnail
> Wouldn't this leave the thumbnail lying around after the GC cycle, since
> once the system is done with the parent instance, the refcount falls to
> zero, it's cleaned out, and the thumbnail is orphaned, creating a memory
> leak?
If I'd want a per request created and garbaged Image instance I'd do 
something like this :

class myclass(Image):
     def thumbnail(self) :
	"""a doc"""
          return Image(id='thumbnail')

But if you want a per database transaction (it should make some valuable 
cache for each http connection) created and garbaged objects you should 
  use zope volatile (_v_) attribute :

class myclass(Image):
     def thumbnail(self) :
	"""a doc"""
          if not hasattr(self, '_v_thumb') :
                   self._v_thumb = Image(id='thumbnail')
          return self._v_thumb

> I was told at some point that this was OK, but now that I'm hunting memory
> leaks and learning more about that, this no longer seems sensible.
> And since I do it all over the place, it'd explain my horrible leak of
> AcquisitionWrappers and HTTPRequests and so on ... As well as high refcount
> for CMFImage.Image ...

Memory leaks appears in python in only one case : if two objects 
mutually reference them and are not referenced elsewhere.
In zope this cannot happen as any object orphaned in the ZODB tree will 
be garbaged.

More information about the Zope mailing list