[PATCH] hgwebdir: return memory to the OS after each request

Antoine Pitrou solipsis at pitrou.net
Mon Jul 2 20:10:39 CDT 2012


# HG changeset patch
# User Antoine Pitrou <solipsis at pitrou.net>
# Date 1341270506 -7200
# Node ID 41cb7af1d92aa7c898634bfd49fea06388bcf666
# Parent  f7a2849ef8cdd0ff3662b300702e40d55109d49b
hgwebdir: return memory to the OS after each request

hgwebdir doesn't cache repositories, but memory can nevertheless
build up accross requests due to delayed deallocation by Python's
memory management routines.
By forcing invalidation of caches and garbage collection after
each request forwarded to hgweb, we manage to eliminate memory
retention on hgwebdir instances on a (Python 2.7.3, Linux) setup.

Average memory consumption is now around 1.5 GB on hg.python.org
(a mod_wsgi setup with daemon processes running hgwebdir instances),
while it was around 4 GB before this patch.

diff --git a/mercurial/hgweb/hgwebdir_mod.py b/mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py
+++ b/mercurial/hgweb/hgwebdir_mod.py
@@ -6,7 +6,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import os, re, time
+import gc, os, re, time
 from mercurial.i18n import _
 from mercurial import ui, hg, scmutil, util, templater
 from mercurial import error, encoding
@@ -202,7 +202,12 @@
                         req.env['REPO_NAME'] = virtualrepo
                         try:
                             repo = hg.repository(self.ui, real)
-                            return hgweb(repo).run_wsgi(req)
+                            try:
+                                return hgweb(repo).run_wsgi(req)
+                            finally:
+                                repo.invalidate()
+                                del repo
+                                gc.collect()
                         except IOError, inst:
                             msg = inst.strerror
                             raise ErrorResponse(HTTP_SERVER_ERROR, msg)


More information about the Mercurial-devel mailing list