mem0 icon indicating copy to clipboard operation
mem0 copied to clipboard

Bug: Memory leak and thread leak issues in mem0 library

Open cis1124 opened this issue 4 months ago • 2 comments

🐛 Describe the bug

Environment

  • Python 3.10
  • mem0ai==0.1.115
  • pymilvus==2.3.2
  • Using Milvus as vector store with Azure OpenAI as LLM

Problem

  • Memory Leak: Each time Memory.from_config() is called, memory usage increases and never gets released.
  • Thread Leak: New threads are created and never terminated,I found the cause is about PostHog module.
  • Resource Leak: MilvusClient connections and other resources are not properly cleaned up.

Code

@memory_router.get(path="/test/", summary="Api test", response_model=MemoryResponse)
def test_api():
    import psutil
    mm = Memory.from_config(DEFAULT_MEM0RY_CONFIG)
    result = mm.add(messages=[{"role": "user", "content": "test"}],agent_id="test")
    print(f"Memory usage after: {psutil.Process().memory_info().rss/1024/1024}MB")
    return MemoryResponse(message="Api test", code=0, success=True, result=[], token_usage={})

Any solution for this, is there a clean method to clean the VectorDB connections and LLM client?

cis1124 avatar Aug 27 '25 09:08 cis1124

Has v0.1.117 fixed this issue?

elepherai avatar Sep 03 '25 18:09 elepherai

We are running v0.1.118 and see a huge amount of threads from posthog on an app that has very low to no traffic. It regularly kills our containers and forces a restart. We are not directly using posthog. It's only required by mem0.

hrisp avatar Oct 16 '25 09:10 hrisp