Repository navigation
Reduce idle memory by lazy-loading PyGithub and requests - #151
Merged
Merged
Conversation
backup_service imported GitPython (unused - clones use the git CLI), PyGithub and requests at module level, so the gunicorn worker kept them loaded while idle. Import PyGithub/requests only inside the methods that use them and drop the unused GitPython import and dependency. Measured idle gunicorn worker RSS: ~82 MB -> ~58 MB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WiQL2WwE6y3RnK8p6LncYz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The container uses ~150 MB RAM while idle.
backup_service.pyimports three heavy libraries at the top of the module, so the gunicorn worker keeps them loaded even when nothing is running:import git(GitPython) is never used. Clones already shell out to thegitCLI.from github import Github(PyGithub also pulls in PyJWT, cryptography, PyNaCl and urllib3)import requestsChanges
import gitand droppedgitpythonfromrequirements.txt.from github import Githubandimport requestsinto the methods that use them (_download_releases,verify_github_access,_download_file).app.pyalready imported PyGithub this way in the add-by-username route.No behaviour change. The modules load the first time a backup or access check needs them.
Measurements
Measured with gunicorn running exactly as
start.shdoes (--workers 1 --threads 4), after startup plus a few requests:That is about 25–30 MB less (~28%). Inside Docker the totals are higher because of the Python 3.14 image and page cache, but the saving is the same size. This is idle memory after startup. Once a backup has run, PyGithub and requests stay loaded until the container restarts.
Also tried
MALLOC_ARENA_MAX=2: no measurable difference at idle, so it's not included.Testing
test_fixes.pypasses (template syntax and app import).github,git,requests,urllib3,jwtorcryptographyat import time.verify_github_access()against a real repo: it succeeded._download_releases()and_download_file()against the GitHub API: both worked.🤖 Generated with Claude Code
https://claude.ai/code/session_01WiQL2WwE6y3RnK8p6LncYz
Generated by Claude Code