Mercurial > hg
view mercurial/scmwindows.py @ 47010:76ae43d5b1db stable
repoview: fix memory leak of filtered repo classes
The leak occurs in long-running server processes with
extensions, and is measured at 110kB per request.
Before this change, the contents of the `_filteredrepotypes`
cache are not properly garbage collected, despite it begin
a `WeakKeyDictionary`.
Extensions have a tendency to generate a new repository class
for each `localrepo` instantiation. Server processes based
on `hgwebdir_mod` will instantiate a new `localrepo` for each
HTTP request that involves a repository.
As a result, with a testing process that repeatedly opens a
repository with several extensions activated
(`topic` notably among them), we see a steady increase in
resident memory of 110kB per repository instantiation before this
change. This is also true, if we call `gc.collect()` at each
instantiation, like `hgwebdir_mod` does, or not.
The cause of the leak is that the *values* aren't weak references.
This change uses `weakref.ref` for the values, and this makes
in our measurements the resident size increase drop to 5kB per
repository instantiation, with no explicit call of `gc.collect()`
at all.
There is currently no reason to believe that this remaining leak
of 5kB is related to or even due to Mercurial core.
We've also seen evidence that `ui.ui` instances weren't properly
garbage collected before the change (with the change, they are).
This could explain why the figures are relatively high.
In theory, the collection of weak references could lead to
much more misses in the cache, so we measured the impact on
the original case that was motivation for introducing that cache
in 7e89bd0cfb86 (see also issue5043): `hg convert` of the
mozilla-central repository. The bad news here is that there is a
major memory leak there, both with and without the present changeset.
There were no more cache misses, and we could see no
more memory leak with this change: the resident size after importing
roughly 100000 changesets was at 12.4GB before, and 12.5GB after.
The small increase is mentioned for completeness only, and we
believe that it should be ignored, at least as long as the main
leak isn't fixed. At less than 1% of the main leak, even finding out
whether it is merely noise would be wasteful.
Original context where this was spotted and first mitigated:
https://foss.heptapod.net/heptapod/heptapod/-/issues/466
The leak reduction was also obtained in Heptapod inner HTTP server,
which amounts to the same as `hgwebdir_mod` for these questions.
The measurements done with Python 3.9, similar figures seen with 3.8.
More work on our side would be needed to give measurements with 2.7,
because of testing server process does not support it.
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Fri, 23 Apr 2021 18:30:53 +0200 |
parents | 224af78021de |
children | 6000f5b25c9b |
line wrap: on
line source
from __future__ import absolute_import import os from . import ( encoding, pycompat, util, win32, ) try: import _winreg as winreg # pytype: disable=import-error winreg.CloseKey except ImportError: # py2 only import winreg # pytype: disable=import-error # MS-DOS 'more' is the only pager available by default on Windows. fallbackpager = b'more' def systemrcpath(): '''return default os-specific hgrc search path''' rcpath = [] filename = win32.executablepath() # Use mercurial.ini found in directory with hg.exe progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini') rcpath.append(progrc) def _processdir(progrcd): if os.path.isdir(progrcd): for f, kind in sorted(util.listdir(progrcd)): if f.endswith(b'.rc'): rcpath.append(os.path.join(progrcd, f)) # Use hgrc.d found in directory with hg.exe _processdir(os.path.join(os.path.dirname(filename), b'hgrc.d')) # treat a PROGRAMDATA directory as equivalent to /etc/mercurial programdata = encoding.environ.get(b'PROGRAMDATA') if programdata: programdata = os.path.join(programdata, b'Mercurial') _processdir(os.path.join(programdata, b'hgrc.d')) ini = os.path.join(programdata, b'mercurial.ini') if os.path.isfile(ini): rcpath.append(ini) ini = os.path.join(programdata, b'hgrc') if os.path.isfile(ini): rcpath.append(ini) # next look for a system rcpath in the registry value = util.lookupreg( b'SOFTWARE\\Mercurial', None, winreg.HKEY_LOCAL_MACHINE ) if value and isinstance(value, bytes): value = util.localpath(value) for p in value.split(pycompat.ospathsep): if p.lower().endswith(b'mercurial.ini'): rcpath.append(p) else: _processdir(p) return rcpath def userrcpath(): '''return os-specific hgrc search path to the user dir''' home = _legacy_expanduser(b'~') path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')] userprofile = encoding.environ.get(b'USERPROFILE') if userprofile and userprofile != home: path.append(os.path.join(userprofile, b'mercurial.ini')) path.append(os.path.join(userprofile, b'.hgrc')) return path def _legacy_expanduser(path): """Expand ~ and ~user constructs in the pre 3.8 style""" # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See # https://bugs.python.org/issue36264. It also seems to capitalize the drive # letter, as though it was processed through os.path.realpath(). if not path.startswith(b'~'): return path i, n = 1, len(path) while i < n and path[i] not in b'\\/': i += 1 if b'HOME' in encoding.environ: userhome = encoding.environ[b'HOME'] elif b'USERPROFILE' in encoding.environ: userhome = encoding.environ[b'USERPROFILE'] elif b'HOMEPATH' not in encoding.environ: return path else: try: drive = encoding.environ[b'HOMEDRIVE'] except KeyError: drive = b'' userhome = os.path.join(drive, encoding.environ[b'HOMEPATH']) if i != 1: # ~user userhome = os.path.join(os.path.dirname(userhome), path[1:i]) return userhome + path[i:] def termsize(ui): return win32.termsize()