Skip to content

Conversation

@jairad26
Copy link
Contributor

@jairad26 jairad26 commented Nov 14, 2025

Description of changes

Summarize the changes made by this PR.

  • Improvements & Bug fixes
    • There was a leak of ~1.5MB of memory every time del Client is called with a PersistentClient. This was due to not freeing system instances after no clients use it, meaning it stays in the SharedSystemClient cache. This was solved by having a cache of identifier to clientcount. once the clientcount for a given System reaches 0, that can be freed from memory. Fixes [Bug]: Memory is not freed when using PersistentClient #5843

repro script:

import chromadb
import tempfile
import psutil
import os

process = psutil.Process(os.getpid())

for i in range(100):
    # Create unique temp directory for each iteration
    temp_dir = tempfile.mkdtemp(prefix="chroma_")

    # Create PersistentClient with ~16,500 embeddings (1536 dimensions)
    client = chromadb.PersistentClient(path=temp_dir)
    collection = client.get_or_create_collection("my_collection")

    # Use the collection...
    results = collection.query(query_embeddings=[[0.1] * 1536], n_results=10)

    # Client goes out of scope, but memory is NOT freed
    del client
    del collection

    print(f"RSS after iteration {i+1}: {process.memory_info().rss / 1024**2:.2f} MB")

  • New functionality
    • ...

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@jairad26 jairad26 force-pushed the jai/free-system-memory-after-client-del branch from fe4ff17 to a276e4e Compare November 14, 2025 17:19
@jairad26 jairad26 marked this pull request as ready for review November 14, 2025 17:20
@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Nov 14, 2025

Reference-counted lifecycle management for SharedSystemClient

Fixes a persistent memory leak (~1.5 MB per PersistentClient) by tracking how many clients reference each cached System and tearing the System down when the count reaches zero. Adds explicit cleanup APIs and makes all cache and ref-count mutations thread-safe via a class-level lock.

Key Changes

• Added _identifier_to_clientcount and _count_lock to SharedSystemClient for thread-safe reference counting
• Increment count in __init__; new _decrement_refcount decrements, stops the System, and removes cache entries when count hits 0
• Introduced close, context-manager (__enter__/__exit__), and fallback __del__ to ensure cleanup paths
• Wrapped all accesses/mutations of _identifier_to_system and _identifier_to_clientcount in with _count_lock blocks (_create_system_if_not_exists, _populate_data_from_system, _system, clear_system_cache)
• Updated imports to include threading

Affected Areas

chromadb/api/shared_system_client.py
• Lifecycle handling of System instances
• Memory footprint when using PersistentClient

This summary was automatically generated by @propel-code-bot

@jairad26 jairad26 force-pushed the jai/free-system-memory-after-client-del branch from a276e4e to bb7d586 Compare November 14, 2025 17:44
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.

[Bug]: Memory is not freed when using PersistentClient

2 participants