Open hmaarrfk opened 1 year ago
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
No stale
hi @hmaarrfk
Have you found any other solution besides explicitly closing containers and streams?
This is my current code that encodes and decodes from the list of numpy array images (imgs_padded
). It still produces memory leaks:
with io.BytesIO() as buf:
with av.open(buf, "w", format=container_string) as container:
stream = container.add_stream(codec, rate=rate, options=options)
stream.height = imgs[0].shape[0]
stream.width = imgs[0].shape[1]
stream.pix_fmt = pixel_fmt
for img in imgs_padded:
frame = av.VideoFrame.from_ndarray(img, format="rgb24")
frame.pict_type = "NONE"
for packet in stream.encode(frame):
container.mux(packet)
# Flush stream
for packet in stream.encode():
container.mux(packet)
stream.close()
outputs = []
with av.open(buf, "r", format=container_string) as video:
for i, frame in enumerate(video.decode(video=0)):
if sequence_length <= i < sequence_length * 2:
outputs.append(frame.to_rgb().to_ndarray().astype(np.uint8))
if i >= sequence_length * 2:
break
video.streams.video[0].close()
no, i just explicitly call close.
Ok, thanks.
I later found out the memory leak only occurs if I use the libx265
codec. Everything is ok when using libx264, mpeg2video, mpeg1video, libvpx-vp9
.
I can also reproduce this, without stream.close()
the memory leaks.
It seems to happen when I process the frames in a different process, and stream.close causes everything to hang if I use more than 1 processes. Multiple bugs bundled into one 😅
hi, @meakbiyik Have you found any solution to deal with the problem? I find that if i break before all the frame extracted, stream.close causes everything to hang
Hey @RoyaltyLJW, I still use stream.close()
, and I was able to fix the deadlock issue by setting the environment variable PYAV_LOGGING=off
. Here's that bug: https://github.com/PyAV-Org/PyAV/issues/751
@meakbiyik Thanks a lot. It fix my deadlock issue
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Not stale
Overview
I think there is a memory leak that occurs if you don't explicitly close a container or stream.
I'm still trying to drill the problem down, but I think I have a minimum reproducing example that I think is worthwhile to share at this stage.
It seems that
__dealloc__
isn't called as expected maybe??? https://github.com/PyAV-Org/PyAV/blob/main/av/container/input.pyx#L88Expected behavior
That the memory be cleared. If I add the![image](https://user-images.githubusercontent.com/90008/228960278-92c91a36-5cd1-47e9-92a8-8f9fcaefddcf.png)
close
calls to the loop I get.Versions
My PyAV builds include the patch in https://github.com/PyAV-Org/PyAV/pull/1061 those in master. I really don't think it is related, but if you want, I'll run my plotting code again.
Research
I have done the following:
Additional context
Maybe related to: