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 = func.__name__
    def __get__(self, obj, type=None):
        result = self.func(obj)
        setattr(obj,, 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
<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
Mercurial version: 6f21613d25a2
([], [], [], [], [], [], [])
gc.collect() returns 152

gc.collect returns the number of unreachable objects found.

See also