Memory leak on Linux
- 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.
i also find memory leak on Linux
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
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.