fix: Optimize sync memory usage to prevent OOM on large projects by phernandez · Pull Request #380 · basicmachines-co/basic-memory · GitHub
Skip to content

fix: Optimize sync memory usage to prevent OOM on large projects - #380

Merged
phernandez merged 1 commit into
mainfrom
fix/sync-memory-optimization
Oct 17, 2025
Merged

fix: Optimize sync memory usage to prevent OOM on large projects#380
phernandez merged 1 commit into
mainfrom
fix/sync-memory-optimization

Conversation

@phernandez

Copy link
Copy Markdown
Member

Summary

Implements P1 and P2 memory optimization fixes from basicmachines-co/basic-memory-cloud#198 to prevent OOM kills on large projects.

Changes

P1: Semaphore for Concurrent File Processing

  • Added asyncio.Semaphore to limit concurrent file operations (default: 10 files)
  • Wrapped _read_file_async() and _compute_checksum_async() with semaphore
  • Added sync_max_concurrent_files configuration option

P2: LRU Cache for File Failure Tracking

  • Changed _file_failures from Dict to OrderedDict for LRU behavior
  • Added max size limit of 100 entries with automatic eviction
  • Updated _record_failure() to enforce cache size and maintain LRU ordering

Documentation

  • Updated sync_thread_pool_size description to note optimization for 1-2GB cloud deployments
  • Added documentation for new sync_max_concurrent_files parameter

Impact

Memory Reduction:

  • Before: 2,621 files could all load into memory simultaneously
  • After: Maximum 10 files in memory at once (configurable)
  • Expected reduction: 90%+ on large projects

OOM Prevention:

  • Tenant with 2,621 files previously consumed 1.36GB and got OOM killed on 1GB machine
  • With these changes, should stay under 800MB on 1GB machines

Configuration:
Users can tune memory usage via config:

{
  "sync_thread_pool_size": 4,           // Thread pool workers (default: 4)
  "sync_max_concurrent_files": 10       // Max concurrent files (default: 10)
}

For very constrained environments, sync_max_concurrent_files can be lowered to 5.

Test Plan

  • All existing tests pass
  • Deploy to tenant-6d2ff1a3 (2,621 files) and verify memory stays under 1GB
  • Monitor sync performance to ensure semaphore doesn't significantly impact speed

Related Issues

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

Implements P1 and P2 fixes from basicmachines-co/basic-memory-cloud#198

Changes:
- Add semaphore to limit concurrent file processing (default: 10 files max)
- Add LRU cache with bounded size for file failure tracking (max 100 entries)
- Add sync_max_concurrent_files configuration option
- Update sync_thread_pool_size documentation

Impact:
- Reduces peak memory usage by 90%+ on large projects (2000+ files)
- Prevents OOM kills on 1GB machines with projects containing 2,621 files
- Before: All files could load into memory simultaneously
- After: Maximum 10 files in memory at once (configurable)

Related: basicmachines-co/basic-memory-cloud#198

Signed-off-by: Claude <noreply@anthropic.com>
Signed-off-by: phernandez <paul@basicmachines.co>
@phernandez
phernandez force-pushed the fix/sync-memory-optimization branch from 56f54fc to 772ddbf Compare October 17, 2025 00:57
@github-actions

github-actions Bot commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

@phernandez
phernandez merged commit 4fd6d0c into main Oct 17, 2025
16 checks passed
@phernandez
phernandez deleted the fix/sync-memory-optimization branch October 17, 2025 01:17
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.

1 participant