decord icon indicating copy to clipboard operation
decord copied to clipboard

Memory leak on Linux

Open se7enXF opened this issue 1 year ago • 3 comments

  • version: 0.6.0
  • Windows: pycharm 2023.3.1 Community Edition, python 3.9
  • Linux: RHEl 7.5, python 3.9
  • problem: memory leak on Linux but not on Windows

Read frame codes:

from decord import VideoReader
reader = VideoReader(url)
frame_count = len(reader)
frame_ids = np.linespace(0, frame_count  - 1, 10, dtype=int).tolist()
frames = reader.get_batch(frame_ids).asnumpy()

I use decord to read video frames in a Fastapi view. There is no memory leak on Windows when debuging in pycharm, but leak on Linux when start python script directly. Memory only raise when requeset the api, without any signs of decreasing.

To debug memory leak, I call reader.del() after read frames. It works on Windows but meets Segmentation fault on Linux.

Finally, I use numpy.zeros() creating frames to replace decord reading frames, there is no memory leak.

Someone help me please.

se7enXF avatar Oct 24 '24 03:10 se7enXF

i also find memory leak on Linux

ITerydh avatar Nov 22 '24 10:11 ITerydh

Simply calling del reader may not reliably clean up resources on Linux. Instead:

Set reader = None after reading frames. Call gc.collect() to prompt Python’s garbage collector to free memory. For example:

from decord import VideoReader, cpu import gc

def read_frames(url): reader = VideoReader(url, ctx=cpu(0)) frame_count = len(reader) frame_ids = np.linspace(0, frame_count - 1, 10, dtype=int).tolist() frames = reader.get_batch(frame_ids).asnumpy() # Release references del frames reader = None gc.collect() return True

NeighborhoodCoding avatar Dec 09 '24 04:12 NeighborhoodCoding

I simply try to read frames as batches using

vr = VideoReader(str(input), ctx=cpu(0))
num_frames = len(vr)
batch_size = 32

for i in range(0, num_frames, batch_size):
    frames = vr.get_batch(range(i, min(i + batch_size, num_frames))).asnumpy()
    print(frames.shape)

This is the entire code and also leaks heavily (filling 64GiB of memory within seconds) - it just seems there is no garbage collection happening at all. Using del frames should not and does not help here.

richard-vock avatar Mar 11 '25 13:03 richard-vock