client-sdk-unity icon indicating copy to clipboard operation
client-sdk-unity copied to clipboard

Memory Leak in TextureVideoSource.Update()

Open rubyknickerbocker opened this issue 5 months ago • 2 comments

Summary

TextureVideoSource.Update() does not get cleaned up properly by TextureVideoSource.Stop() and TextureVideoSource.Dispose() are called from Unity's OnApplicationQuit(). Basically, if video is still being streamed when you stop the app (from in the Unity editor), it causes a memory leak. This does not happen if I stop streaming or leave the room before ending the app.

Environment

LiveKit Unity SDK Version: 1.2.5 Unity Version: 6000.1.5f1 Platform: MacOS LiveKit Server Version: 1.9.0

Analysis

The memory leak appears to be in the internal ReadBuffer() method called by source.Update(). The leak cannot be mitigated by:

Proper disposal patterns Garbage collection calls Setting cleanup flags Different RenderTexture formats

Workarounds Attempted

Controlled cleanup timing - Still leaks memory Reduced update frequency - Reduces leak rate but doesn't eliminate it Early termination of Update() calls - Breaks video streaming functionality Alternative RenderTexture formats - No improvement

TLDR

When the app is stopped in the Unity editor with video actively streaming, the TextureVideoSource.Update coroutine does not get cleaned up in time and results in a memory leak. If this is a known issue, or if there's known workaround, please get back to me.

rubyknickerbocker avatar Aug 30 '25 00:08 rubyknickerbocker

Hi @rubyknickerbocker, thank you for the detailed report—we will investigate the issue.

ladvoc avatar Sep 02 '25 05:09 ladvoc

ive also had a similar problem and there are other issues similar to this in unity. from what ive seen it seems to be happening somewhere in the native array relating to using gpu callbacks. the posts related to this are:

https://discussions.unity.com/t/asyncgpureadback-requestintonativearray-causes-invalidoperationexception-on-nativearray/818225/79

https://discussions.unity.com/t/asyncgpureadback-requestintonativearray-what-am-i-doing-wrong/812861

this seems to be a known issue on unity side... so i'm not sure how to go about it. it seems to be more a warning sort of issue rather than serious memory leak as far as i can tell but im quite new to this so i'm not sure.

mmmmmapplepie avatar Sep 10 '25 02:09 mmmmmapplepie