No objects ever released by the GC, potential memory leak?
See original GitHub issueFirst 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.
Here some examples from a real service in k8s via lens metrics:
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:
- Created 2 years ago
- Reactions:1
- Comments:30 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
orrequest.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.I can reproduce it on Python 3.7.13, but it’s not reproducible from 3.8+.
Notes:
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.