Bug: ImageSequenceClip circular referencing leads to memory leak
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.
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.
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
Source: Zulko/moviepy