How to delete leaking objects that are increased in while loops?

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.

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

1 Like

My guess is that the capture is never released (i.e., capture.release() ), thus, no reason for the garbage collector to collect it.

1 Like

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.

1 Like

Thank you for your reply @edsaac @Goyo
I found similar topic in github issues.

Is anyone know that there is any update about it…?
If possible, I would be happy to receive information from @randyzwitch
Many thanks,

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.