Differences between revisions 1 and 2
Revision 1 as of 2009-06-07 09:36:26
Size: 2004
Editor: abuehl
Revision 2 as of 2009-06-07 09:37:33
Size: 2025
Editor: abuehl
Deletions are marked like this. Additions are marked like this.
Line 80: Line 80:
{{gc.collect}} returns the number of unreachable objects found. {{{gc.collect}}} returns the number of unreachable objects found (152 in this case).

Mercurial produces reference cycles in objects that need to be broken up by the gc to clean up unused objects. This is relevant if Mercurial is to be used via its Python API in a server application implemented in Python.

Snippet 1

import os, sys
import gc
from pprint import pprint


class propertycache(object):
    def __init__(self, func):
        self.func = func
        self.name = func.__name__
    def __get__(self, obj, type=None):
        result = self.func(obj)
        setattr(obj, self.name, result)
        return result

class a(object):
    def __init__(self):
        self.dict = {}
    def f(self):
        return len(self.dict)
    def g(self):
        return self.f

b = a()
print b.g, b.g()
b = None

print "gc.collect() returns %s" % gc.collect()

Produces (Python 2.5.1 on Windows):

> python test.py
<bound method a.f of <__main__.a object at 0x00A9B930>> 0
gc: collectable <a 00A9B930>
gc: collectable <dict 00A99150>
gc: collectable <dict 00A99270>
gc: collectable <instancemethod 00A90AA8>
gc.collect() returns 4
[<__main__.a object at 0x00A9B930>,
 {'dict': {}, 'g': <bound method a.f of <__main__.a object at 0x00A9B930>>},
 <bound method a.f of <__main__.a object at 0x00A9B930>>]

Snippet 2

from mercurial import hg, ui, util
import os
import gc

def test():
    print "Mercurial version: %s" % util.version()
    repo = hg.repository(ui.ui(), os.getcwd())
    status = repo.status()
    print status

print "gc.collect() returns %s" % gc.collect()


> python test2.py
Mercurial version: 6f21613d25a2
([], [], [], [], [], [], [])
gc.collect() returns 152

gc.collect returns the number of unreachable objects found (152 in this case).

See also

ReferenceCycles (last edited 2012-10-25 21:34:18 by mpm)