question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No objects ever released by the GC, potential memory leak?

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# From the official documentation
# Run with uvicorn main:app --reload
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

Description

Use the minimal example provided in the documentation, call the API 1M times. You will see that the memory usage piles up and up but never goes down. The GC can’t free any objects. It’s very noticeable once you have a real use case like a file upload that DoS’es your service.

memory-profile

Here some examples from a real service in k8s via lens metrics:

Screenshot 2022-03-04 at 16 47 18 Screenshot 2022-03-03 at 20 19 22

Operating System

Linux, macOS

Operating System Details

No response

FastAPI Version

0.74.1

Python Version

Python 3.10.1

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:30 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
darkcloudercommented, Jul 29, 2022

The memory leak in uvicorn is probably not the cause of my issue though. First of all, it only happens with FastAPI >=0.69.0, and I also had apps where that happens where I don’t even use app.state or request.state at all. I think I will put some more effort in isolating a minimal running version with that memory leak for my case. I’ll get back to you if I manage to do that.

3reactions
Kludexcommented, Sep 3, 2022

I can reproduce it on Python 3.7.13, but it’s not reproducible from 3.8+.

Notes:

  • This issue can be reproduced with pure Starlettte - this means this issue can be closed here.
  • This issue cannot be reproduced on Python 3.8+.
  • This issue cannot be reproduced with Starlette 0.14.2 (version pre-anyio) in Python 3.7.
  • This issue can only be reproduced by Starlette 0.15.0+ on Python 3.7.

I’ll not spend more time on this issue. My recommendation is to bump your Python version.

In any case, this issue doesn’t belong to FastAPI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Everything you need to know about Memory Leaks in Android.
The garbage collector will never release or freeing them up from the memory cause it shows that those objects are in use while...
Read more >
8 Ways You can Cause Memory Leaks in .NET
The first core cause is when you have objects that are still referenced but are effectually unused. Since they are referenced, the GC...
Read more >
Understanding Memory Leaks in Java - Baeldung
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector...
Read more >
How to Detect Memory Leaks in Java: Causes, Types, & Tools
Unused objects are typically removed by the Java Garbage Collector (GC) but in cases where objects are still being referenced, they are not...
Read more >
Memory management and patterns in ASP.NET Core
The GC cannot free objects that are referenced. Objects that are referenced but no longer needed result in a memory leak. If the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found