GKu
February 28, 2023, 11:20am
1
Summary
I would like to show image that is read from OpenCV.
I coded like below, but it looks memory leak occurs.
Could you tell me how to fix it? (Is it possible to delete leaking streamlit objects?)
I identified leaking objects with reference to the link below.
But I could not figure out to delete them.
https://blog.streamlit.io/3-steps-to-fix-app-memory-leaks/#1-identify-the-memory-leak
Steps to reproduce
Code snippet:
import streamlit as st
import cv2
import gc
image_holder = st.empty()
capture = cv2.VideoCapture(0)
while capture.isOpened():
ret, image = capture.read()
#' A new object is created every loops
# and it leads to leaking memory.
image_holder.image(image, channels="BGR")
gc.collect()
Identified leaking objects:
After 243 runs the following traces were collected.
{
β/home/β¦/.local/lib/python3.8/site-packages/streamlit/elements/image.py:223β: 218,
β/home/β¦/.local/lib/python3.8/site-packages/streamlit/elements/image.py:260β: 218,
β/home/β¦/.local/lib/python3.8/site-packages/streamlit/runtime/media_file_manager.py:225β: 218,
β/home/β¦/.local/lib/python3.8/site-packages/streamlit/runtime/media_file_manager.py:45β: 218,
β/home/β¦/.local/lib/python3.8/site-packages/streamlit/runtime/memory_media_file_storage.py:65β: 218
}
Debug info
Streamlit version: 1.19.0
Python version: 3.8.10
Using Conda? PipEnv? PyEnv? Pex? No
OS version: Ubuntu 20.04
Browser version: Firefox 110.0
Requirements file
opencv-python == 4.7.0.68
edsaac
February 28, 2023, 1:45pm
2
My guess is that the capture is never released (i.e., capture.release() ), thus, no reason for the garbage collector to collect it.
Goyo
February 28, 2023, 3:23pm
3
I am under the impression that the widgets that you put in a container are not released until the app reruns, even if you substitute them for other widgets.
It is true that capture is not released either, but I see no reason for it to keep growing in size.
GKu
March 1, 2023, 1:12am
4
Thank you for your reply @edsaac @Goyo
I found similar topic in github issues.
opened 07:24PM - 12 Oct 20 UTC
closed 09:58PM - 30 Nov 21 UTC
type:enhancement
# Summary
When replacing images in the same location, the previous image does⦠not delete until the session is disconnected, This has the potential to create a memory leak.
# Steps to reproduce
```python
foo = st.empty()
for im in [image1, image2, image3, image4, image5]: # For large number of images
foo.image(im)
```
## Expected behavior:
Old images should get deleted from memory file manager
## Actual behavior:
All images remain loaded in `media_file_manager`
## Is this a regression?
No
# Debug info
- Streamlit version: pre-0.69
# Additional information
This is deliberate because it is difficult to detect _when_ an image is used (and at what location). So we do not have a convenient time to delete the image. One solution would be to send the coordinate information with the image (as a query param), so that the server can check if it's included and delete it accordingly. There's still plenty of issues to detect though.
Is anyone know that there is any update about it�
If possible, I would be happy to receive information from @randyzwitch
Many thanks,