Replies: 4 comments 6 replies
This comment has been hidden.
This comment has been hidden.
-
|
What would be needed to move this discussion forwards or upgrade to an issue? @YuriiMotov tagging you because you're an active collaborator of this repository and replied to the comment above. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for sharing all the graphs, I'll check it out. It looks a bit weird that it would consume so much memory as it's computed once and then cached. I imagine maybe if there's something creating new dependencies every time, maybe storing them somewhere, or something like that... I'm trying to figure out in which case could there be something that consumes and keeps consuming so much extra memory, I don't see an obvious answer in the code, nor the functools docs. 🤔 I'll keep checking a bit more to see if I can find anything, but if you or anyone else could create some way to replicate it, it would be very useful. |
Beta Was this translation helpful? Give feedback.
-
|
I opened a PR that should help or close this issue: #15336 |
Beta Was this translation helpful? Give feedback.




Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
We're currently using FastAPI
0.120.4in production which uses less than 400MB of memory.Using memray to profile the total memory usage, I got:

However, when we tried to upgrade to
0.121.3, we found that an out-of-memory (OOM) error was raised.I did a profile of the total memory usage with 0.121.3 and I got:

When I dig deeper in the flamegraph, I found the following difference between the two versions.

With 0.120.4:
With 0.121.3:

As you can see from the flamegraph, 0.121.3 shows more memory consumption from
get_flat_dependantfunction.Also, running memray in live mode showed the following:

As we can see there,
__get__fromfunctoolsis consuming a lot of memory.So, I checked the PR
scopewas introduced for dependencies and I found the following change wherefunctools.cached_propertywas introduced: https://github.com/fastapi/fastapi/pull/14262/files#diff-4d1d65ce33d5ef7383c87f7edeb3928bc0537016daba6d1a6b8b27a7af8f8aaaR46-R52With all this information, I did two tests.
cache_keyas apropertycache_keyto use__post__init__:As you can see in both examples, total memory consumption was reduced to something very similar as version
0.120.4.Lastly, I also tried running FastAPI with all versions from 0.120 to the latest 0.128 in Docker and I got the following resources usage metrics:

Also, this shows an increment of memory usage. I also noticed that in the latest versions more cached properties were introduced, so changing only
cache_keyis not enough and more cached properties need to be updated.From the information I was able to gather, I can't really say if this is a memory leak or just more memory consumption.
Also, I wasn't able to reproduce this with a minimal example yet since I believe the issue is noticeable when many dependencies are used.
I'd appreciate any help of feedback about this issue.
Thanks
Operating System
Linux, macOS
Operating System Details
No response
FastAPI Version
0.121.3
Pydantic Version
2.12.5
Python Version
3.12.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions