diff --git a/hgitaly/service/repository.py b/hgitaly/service/repository.py
index 9787c1a7b9390e3f09babce1506254eb698dfba3_aGdpdGFseS9zZXJ2aWNlL3JlcG9zaXRvcnkucHk=..6576937ea5a7c7e127de71c579251738487f5406_aGdpdGFseS9zZXJ2aWNlL3JlcG9zaXRvcnkucHk= 100644
--- a/hgitaly/service/repository.py
+++ b/hgitaly/service/repository.py
@@ -4,6 +4,7 @@
 # GNU General Public License version 2 or any later version.
 #
 # SPDX-License-Identifier: GPL-2.0-or-later
+import gc
 from grpc import StatusCode
 import itertools
 import logging
@@ -211,16 +212,20 @@
         with tempfile.TemporaryFile(
                 mode='wb+',  # the default, but let's insist on binary here
                 buffering=WRITE_BUFFER_SIZE) as tmpf:
-            archival.archive(
-                repo,
-                tmpf,
-                ctx.node(),
-                ARCHIVE_FORMATS[request.format],
-                True,  # decode (TODO this is the default but what is this?)
-                match,
-                request.prefix.encode(),
-                subrepos=False  # maybe later, check what GitLab's standard is
-            )
+            try:
+                archival.archive(
+                    repo,
+                    tmpf,
+                    ctx.node(),
+                    ARCHIVE_FORMATS[request.format],
+                    # TODO this is the default but check what it means:
+                    True,  # decode
+                    match,
+                    request.prefix.encode(),
+                    subrepos=False  # maybe later, check GitLab's standard
+                )
+            finally:
+                gc.collect()
 
             tmpf.seek(0)
             while True:
@@ -490,9 +495,12 @@
         with tempfile.NamedTemporaryFile(
                 mode='wb+',
                 buffering=WRITE_BUFFER_SIZE) as tmpf:
-            with repo.ui.configoverride(overrides, b'CreateBundle'):
-                bundle(repo.ui, repo, pycompat.sysbytes(tmpf.name),
-                       **bundle_opts)
+            try:
+                with repo.ui.configoverride(overrides, b'CreateBundle'):
+                    bundle(repo.ui, repo, pycompat.sysbytes(tmpf.name),
+                           **bundle_opts)
+            finally:
+                gc.collect()
             tmpf.seek(0)
             while True:
                 data = tmpf.read(WRITE_BUFFER_SIZE)
@@ -564,6 +572,7 @@
             ) as (repo, tmpf):
                 unbundle(repo, tmpf.name)
         finally:
+            gc.collect()
             return CreateRepositoryFromBundleResponse()
 
     def CreateRepositoryFromBundle(
@@ -592,6 +601,8 @@
                 shutil.rmtree(repo.root)
                 context.abort(StatusCode.INTERNAL,
                               "error in bundle application: %r" % exc)
+            finally:
+                gc.collect()
         return CreateRepositoryFromBundleResponse()
 
     def hg_init_repository(self, repository: Repository, context):