#2437·moviepy

Bug: ImageSequenceClip circular referencing leads to memory leak

Author: timote-MOREAUXCreated May 16, 2025Updated May 23, 2026
Labelsbugimagesperformance

Expected Behavior

ImageSequenceClip is automatically collected by the garbage collector after it is de referenced.

Actual Behavior

ImageSequenceClip is not collected leading to a potential severe memory leakage as all frames cannot be collected either.

Steps and code to Reproduce the Problem

Creating many different ImageSequenceClip consecutively will lead to memory leakage. See https://github.com/Farama-Foundation/Gymnasium/issues/1322#issuecomment-2741145076 for complete code example.

Cause and remediation

The issue may be due to a circular referencing between sequence variable and frame_function function in ImageSequenceClip. A simple way to solve the issue would be to implement the close method and break the circular referencing.

python3
def close(self):
    """Close the clip and free the memory."""
    # Deleting one of the two should work
    del self.sequence
    # Or / and
    del self.frame_function
    return super().close()

Here is the result of the code provided earlier in the comment using close method and implementing the close method.

Image

Another solution is to refactor the entire class to remove circular referencing.

Specifications

Issue found on:

  • Python Version: 3.10.16
  • MoviePy Version: 2.1.2
  • Platform Name: Ubuntu
  • Platform Version: 24.04

Issue confirmed on:

  • Python Version: 3.11.2
  • MoviePy Version: 2.1.2
  • Platform Name: Debian
  • Platform Version: 12