New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Increasing memory (possible leak, inner cache mechanism) #6510
Comments
Thank you for doing this, @jakubxy08. I've noticed an increase in our long-running app as well, but have never been able to isolate it. Am quite surprised to hear that only having line 33 would leak as well. That line doesn't do anything but call empty() on the chart_container element. |
Just the fact that also "container.empty()" called in a loop also causes an increase in memory puzzled me a lot, so I decided to check what "container.empty()" and "container.plotly_chart" have in common. It is the "_enqueue()" method in streamlit.delta_generator.py, which passes the "element_proto: Message" protobuf message, if I'm not mistaken. So is it possible that not all resources are released until the main message finishes: await called in streamlit.runtime.runtime.py lines 577-616 (I was inspired by the protocolbuffers/protobuf#5737 thread). So would changing something here can help, or using a different version of protobuf (latest 4.22, steamlit uses protobuf>=3.19, <4)? I don't have experience with this, so I don't know how to test it, or if what I wrote makes any sense. I would appreciate any comments |
A small update, finally after many attempts I was able to test with a newer version of protobuf, but it did not do anything. In addition, I checked whether deleting proto messages, and intermediate variables will in any way affect memory consumption (I tested on versions streamlit 1.17.0 - 1.21.0 x python 3.8 - 3.10 x windows and Ubuntu) - unfortunately, as before, memory consumption increases steadily. I have no idea what else could be causing this. Related to this and this (#6354 (comment)), I would like to propose some new enchacement () related to reducing memory consumption for long living apps. Maybe as more people are interested in it, it will get some higher priority ;) |
Thanks for the investigation on this + the new enhancement issue @jakubxy08! I'm still trying to track down some memory leaks related to this issue and #6354, but even when those are addressed, I do think that one of the fundamental reasons these are slipping by (both for this issue as well as #6354) is that Streamlit currently isn't designed to support the long-lived script use-case very well. I do have a workaround for you for this particular issue, though. It turns out the high memory growth from calling the We have a hidden config option |
Update: I have a PR (#6617) out fixing the memory leak with our There's still the issue of Streamlit not playing very nicely with super long-lived sessions, but I think it'll be more appropriate to discuss that in #6602. In the meantime, I think the config option workaround that I described above should prevent memory growth caused by the |
I have the same issue I think (version 1.3). The code below is the simplest I could come up with to demonstrate it. With the image shown the memory usage increases rapidly into the GB regime. However, even without the video shown, the memory increases because of the np.random call. Restarting the script causes the memory to become free!
|
Having the same issue. When displaying multiple images, page update starts to become slower and slower. |
Checklist
Summary
Good evening to all streamlit users and developers.
I wanted to find out how I can clear the cache or remove objects from streamlit memory so that when running a long running application that updates charts every e.g. 15 seconds, I can enjoy a fairly constant memory consumption rather than a constant increase.
In the code below if we run it uncommented line 34 or 35 or even only 33, we have increasing memory consumption. On the other hand, commenting all lines 33-35, we have constant memory consumption.
If I understand correctly, this is not a problem of plotly just streamlit and its internal caching mechanism. The program behaved similarly even when setting the decorators @st.cache, @st.cache_data, @st.cache_resource, and their internal parameters (e.g. ttl).
To monitor memory, I used memory_profiler and tremalloc as described here: (https://blog.streamlit.io/3-steps-to-fix-app-memory-leaks/).
The code was tested on both Windows 10, Windows 11, Ubuntu 20.04 and all versions of streamlit from 1.17.0 - 1.21.0.
I am very much asking for help
Reproducible Code Example
Steps To Reproduce
streamlit run filename.py
Expected Behavior
Constant usage of memory
Current Behavior
Increasing memory over time.
Is this a regression?
Debug info
Additional Information
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: