Skip to content

Reduce idle memory by lazy-loading PyGithub and requests - #151

Merged
GitTimeraider merged 1 commit into
developfrom
perf/reduce-idle-memory
Oct 1, 2026
Merged

GitTimeraider merged 1 commit into
developfrom
perf/reduce-idle-memory

Conversation

@GitTimeraider

Copy link
Copy Markdown
Owner

Problem

The container uses ~150 MB RAM while idle. backup_service.py imports 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 the git CLI.
  • from github import Github (PyGithub also pulls in PyJWT, cryptography, PyNaCl and urllib3)
  • import requests

Changes

  • Removed the unused import git and dropped gitpython from requirements.txt.
  • Moved from github import Github and import requests into the methods that use them (_download_releases, verify_github_access, _download_file). app.py already 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.sh does (--workers 1 --threads 4), after startup plus a few requests:

Process Before (RSS) After (RSS)
gunicorn master ~25 MB ~25 MB
gunicorn worker ~82 MB ~58 MB
Total (PSS) ~96 MB ~69 MB

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.py passes (template syntax and app import).
  • Checked that the app no longer loads github, git, requests, urllib3, jwt or cryptography at import time.
  • Ran verify_github_access() against a real repo: it succeeded.
  • Ran _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

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
@GitTimeraider
GitTimeraider merged commit d6c3a9b into develop Oct 1, 2026
1 check passed
@GitTimeraider
GitTimeraider deleted the perf/reduce-idle-memory branch October 1, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants