Skip to content

Commit 60ca325

Browse files
committed
Add option to skip the GC cycle on Repo close
In large environment with thousands of repos and frequent repo opens and closes, the cost of doing 2 full gc cycles on every Repo destruction can become crippling.
1 parent c23ae3a commit 60ca325

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

git/repo/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ class Repo(object):
9090
# Subclasses may easily bring in their own custom types by placing a constructor or type here
9191
GitCommandWrapperType = Git
9292

93-
def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=False):
93+
def __init__(
94+
self, path=None, odbt=DefaultDBType,
95+
search_parent_directories=False,
96+
gc_on_close=True):
9497
"""Create a new Repo instance
9598
9699
:param path:
@@ -113,9 +116,15 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
113116
114117
Please note that this was the default behaviour in older versions of GitPython,
115118
which is considered a bug though.
119+
:param gc_on_close:
120+
Whether to force gc collect and mmap collect on Repo destruction.
121+
In some high-volume cases, the cost of doing frequent full gc cycles
122+
is extremely expensive. While doing the gc is the safe bet, skipping
123+
it can be useful in specific cases. Defaults to True.
116124
:raise InvalidGitRepositoryError:
117125
:raise NoSuchPathError:
118126
:return: git.Repo """
127+
self._gc_on_close = gc_on_close
119128
epath = path or os.getenv('GIT_DIR')
120129
if not epath:
121130
epath = os.getcwd()
@@ -191,9 +200,10 @@ def __del__(self):
191200
def close(self):
192201
if self.git:
193202
self.git.clear_cache()
194-
gc.collect()
195-
gitdb.util.mman.collect()
196-
gc.collect()
203+
if self._gc_on_close:
204+
gc.collect()
205+
gitdb.util.mman.collect()
206+
gc.collect()
197207

198208
def __eq__(self, rhs):
199209
if isinstance(rhs, Repo):

0 commit comments

Comments
 (0)