Memory Management

This section provided additional info and strategies how to manage memory in Chroma.

LRU Cache Strategy

Out of the box Chroma offers an LRU cache strategy which unloads segments (collections) that are not used while trying to abide to the configured memory usage limits.

To enable the LRU cache the following two settings parameters or environment variables need to be set:

PythonEnvironment Variables

  1. from chromadb.config import Settings
  2. settings = Settings(
  3. chroma_segment_cache_policy="LRU",
  4. chroma_memory_limit_bytes=10000000000 # ~10GB
  5. )
  1. export CHROMA_SEGMENT_CACHE_POLICY=LRU
  2. export CHROMA_MEMORY_LIMIT_BYTES=10000000000 # ~10GB

Manual/Custom Collection Unloading

Local Clients

The below code snippets assume you are working with a PersistentClient or an EphemeralClient instance.

At the time of writing (Chroma v0.4.22), Chroma does not allow you to manually unloading of collections from memory.

Here we provide a simple utility function to help users unload collections from memory.

Internal APIs

The below code relies on internal APIs and may change in future versions of Chroma. The function relies on Chroma internal APIs which may change. The below snippet has been tested with Chroma 0.4.24+.

  1. import gc
  2. import os
  3. import chromadb
  4. import psutil
  5. from chromadb.types import SegmentScope
  6. def bytes_to_gb(bytes_value):
  7. return bytes_value / (1024 ** 3)
  8. def get_process_info():
  9. pid = os.getpid()
  10. p = psutil.Process(pid)
  11. with p.oneshot():
  12. mem_info = p.memory_info()
  13. # disk_io = p.io_counters()
  14. return {
  15. "memory_usage": bytes_to_gb(mem_info.rss),
  16. }
  17. def unload_index(collection_name: str, chroma_client: chromadb.PersistentClient):
  18. """
  19. Unloads binary hnsw index from memory and removes both segments (binary and metadata) from the segment cache.
  20. """
  21. collection = chroma_client.get_collection(collection_name)
  22. collection_id = collection.id
  23. segment_manager = chroma_client._server._manager
  24. for scope in [SegmentScope.VECTOR, SegmentScope.METADATA]:
  25. if scope in segment_manager.segment_cache:
  26. cache = segment_manager.segment_cache[scope].cache
  27. if collection_id in cache:
  28. segment_manager.callback_cache_evict(cache[collection_id])
  29. gc.collect()

Example Contributed

The above example was enhanced and contributed by Amir (amdeilami) from our Discord comminity. We appreciate and encourage his work and contributions to the Chroma community.

Usage Example

  1. import chromadb
  2. client = chromadb.PersistentClient(path="testds-1M/chroma-data")
  3. col=client.get_collection("test")
  4. print(col.count())
  5. col.get(limit=1,include=["embeddings"]) # force load the collection into memory
  6. unload_index("test", client)

June 1, 2024