Python Flask app file upload memory leak
I'm running a Python Flask app. When I upload a file to be stored on the server, the web service's memory usage increases by the size of the file (essentially a memory leak) and when I delete the file on the server, the memory is freed. What should happen is the memory doesn't increase significantly when the file is written to the disk.
I tried this on some other platforms like Fly.io and it handled it with no problem (Fly.io's trial machine is 256MB and I uploaded multiple 200MB files in a row without it crashing).
here's some code that shows how I handle each kind of upload
project id:
15154147-a9b4-412c-9f4a-c690ca66a087
12 Replies
Project ID:
15154147-a9b4-412c-9f4a-c690ca66a087
your memory leak wouldn't be a fault of railway's
this would be an issue with your code
Hi Brody, thanks for the quick response, however, I still think it is a problem with Railway. The same code doesn't cause the memory leak described above on fly.io, render.com, local docker container, local machine, etc.
the code is simply getting the file from a post request and saving it to the disk, something like
notes on the
.save()
method:
Save the file to a destination path or file object. If the destination is a file object you have to close it yourself after the call. The buffer size is the number of bytes held in memory during the copy process. It defaults to 16KB.Using
with open()
to write the file using a chunked stream also causes the memory leak. Basically anytime I write a file to the disk it causes the memory leak.okay so besides the fact that you are only able to see this leak on railway, what makes you think its an issue with railway, because so far this is just the "it works on my machine" meme
not saying you're being silly or dumb or anything, but you should provide some kind of proof as to why this is a railway issue, not just "it happens on railway"
What kind of proof would be sufficient
first solve the issue, then tell me what you did to solve it
I solved the issue by using a different hosting provider...
one thing that pops into my mind is maybe this is an issue with the python version railway runs your code with
ive ran services that saved decently sized uploaded files to disk, there was never any increase in memory, this isnt an issue with railway's platform
Probably not because the python version is specified in the Ubuntu image in my dockerfile
if this was an issue with railway itself, i would too see memory spikes during a file upload
Yes that's exactly what happens
...but i dont see any memory spikes in my applications during file uploads
so that means this issue is isolated to your deployment, and not railway