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.

UploadFile hogs RAM

See original GitHub issue

Good day,

We are using FastAPI to access large (1GB) files through starlette UploadFile and we have noticed that all the file is loaded into RAM.

Looking at the code I see that starlette uses SpooledTemporaryFile, however it initializes it [1] with default parameters, namely max_size=0. The thing is that SpooledTemporaryFile.write() methods calls self._check() to switch to disk IO, however with max_size=0 it never does it [2] and hence the whole file is loaded to RAM.

I suggest to starlette’s UploadFile to:

  • Set positive max_size by default
  • Accept user-defined value from constructor

What do you think?

[1] https://github.com/encode/starlette/blob/594b95c1da8c4ed2aff5629ab5ce128a18b6931b/starlette/datastructures.py#L428

[2] https://github.com/python/cpython/blob/master/Lib/tempfile.py#L650

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Jul 16, 2019

Flask (werkzeug) uses 500k by default it seems

Okay, so 1MB would be a good midway point between that and Django’s default.

Will you accept a PR without test for this?

Yup for sure - it’s just exercising a standard bit of Python stdlib, so not an issue at all.

0reactions
haizaarcommented, Jul 16, 2019

PR ready.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - High memory usage when uploading a multipart file to ...
When testing the method with a 25MB file, the memory usage of my Java Spring application in a Kubernetes cluster setup spiked up...
Read more >
RAM being maxed out while uploading huge file
My issue is each time I try to upload the 14.4GB video to YouTube on chrome, opera, firefox. The web-browser crashes.
Read more >
Dropbox using high amount of RAM / memory
I'm really surprised that Dropbox often appears at the top five or four culprits. It takes up over 400 MB, which is insane...
Read more >
Bug #3089: Slow upload / Increase CPU/Memory usage with ...
Our uploads are ~200MB binary files which are handed to a cgi program. The upload took around 1-2min before and after the upload...
Read more >
Home just destroying RAM while using custom import...
Normally when I am in an empty home it uses around 30% of my RAM, which is fine ... But you could upload...
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