io: decode video causes memory leak in ram.
Code to replicate the issue.
import tensorflow as tf
from tensorflow_io.core.python.experimental.ffmpeg_ops import decode_video
def decode_video_profiling(files=[]):
assert len(files) > 0, 'need files to profile'
@tf.function
def file_to_video(video_path):
content = tf.io.read_file(video_path)
frames = decode_video(content, 0)
frames = tf.image.resize(frames, [224, 224])
return frames[:20]
video_paths = tf.data.Dataset.from_tensor_slices(files).repeat()
videos = video_paths.map(file_to_video).batch(16, drop_remainder=True)
memory_profiling(videos)
def memory_profiling(ds, steps=600, stop_and_repeat=False):
from tqdm import tqdm
import psutil
pbar = tqdm(range(steps))
percents = [psutil.virtual_memory().percent]
pbar.set_description_str(f'{percents[-1]}')
pbar.update()
while pbar.n < pbar.total:
for i, dps in enumerate(ds):
if isinstance(dps, tuple):
print(f'{i}:', ','.join([dp.shape for dp in dps]))
else:
print(f'{i}: {dps.shape}')
percents += [psutil.virtual_memory().percent]
pbar.set_description_str(f'Memory usage: {percents[-1]}%')
pbar.update()
if stop_and_repeat or pbar.n >= pbar.total:
break
files = [...] # a bunch of mp4 valid mp4 files which are about 24 fps and 10 seconds long
decode_video_profiling(files)
The code above will print the metrics overtime Note: This is CPU RAM . However I put into a chart,

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (10 by maintainers)
Commits related to this issue
- fix memory leak in decode_video (ref: #680) (#743) — committed to tensorflow/io by kyamagu 4 years ago
- fix memory leak in decode_video (ref: #680) (#743) — committed to i-ony/io by kyamagu 4 years ago
I don’t see obvious memory leak by reading the code. May need to setup leak detector to check. Will take a look.