Skip to content

git gc: too many loose objects error

Quick summary

You ran git gc and it failed because the repository accumulated too many loose objects — packing them exceeded available memory or disk space.

Why this happens

  • The repository has grown very large with millions of loose objects from frequent commits without periodic gc

  • A previous git gc was interrupted, leaving a corrupted or partial pack file that blocks repacking

  • The system has insufficient RAM or disk space for git repack to create the new pack files

Minimal example

✗ Broken code
git gc  # fatal: Out of memory, malloc failed (tried to allocate 2GB)
✓ Fixed code
git repack -d --window=50 --depth=50  # lower memory usage
git prune  # then prune loose objects separately

Reducing the repack window and depth parameters lowers memory consumption, allowing git to pack objects incrementally without exhausting RAM.

How to diagnose

  • Run git count-objects -v to see the number and total size of loose objects in the repository

  • Check available disk space with df -h .git to ensure there is room for new pack files

  • Run git fsck to detect any corrupted objects or partial packs that may be blocking gc

How to fix

  • Run git gc --auto to let git decide if repacking is needed, or git repack -d with a lower window size for memory-constrained systems

  • Remove large files from history with git filter-repo or BFG Repo-Cleaner to reduce the total object count

  • Free up disk space or increase memory, then retry git gc after fixing any corruption found by git fsck

How to prevent

  • Run git gc periodically or rely on the automatic gc trigger after every push to prevent object accumulation

  • Avoid committing large binary files directly — use Git LFS to keep them out of the main object store

Related Resources

Related Glossary

Related Lessons

  • git gc

    Learn about git garbage collection.

← Back to language