[Editor] Fix ALLOC_TEMP_TLS Memory Leaks and Slow Play Mode
Solution
Unity 2019.x - Unity 6.3.x
Published 25 days ago
Persistent ALLOC_TEMP_TLS errors coupled with slow Play Mode initialization and leaks outside of execution indicate exhausted thread-local storage. These errors often persist even after restarting the editor or changing versions, suggesting a serialized logic flaw in your script or asset configuration.
Memory leaks and TLS Allocator errors typically arise from unintended Inspector modifications leading to infinite Debug.Log calls within a recursive loop.
The root cause of TLS Allocator exhaustion is usually the rapid filling of the temporary buffer by high-frequency operations, such as infinite recursion or excessive logging within a single frame. This often happens when Inspector values trigger OnValidate or OnDrawGizmos cycles that never terminate.
- Open the
Consoleand look for the first instance of aStackOverflowExceptionto identify the loop origin. - Locate your script responsible for the recursion and implement a recursion guard or an exit condition.
- Eliminate or gate
Debug.Logcalls that occur inside high-frequency loops or recursive methods to preventTLS Allocatoroverflows. - Reset modified
ScriptableObjectorGameObjectfields in theInspectorto their default states to break circular references.
Additional Tips:
- Check for scripts that run in the editor using the
[ExecuteAlways]attribute, as these can leakTLS Allocatormemory even when not inPlay Mode. - If the issue persists for debugging, use the
-tls-allocator-temp-size=numbercommand-line argument to increase the default buffer size. - Review
OnDrawGizmosimplementations; they execute every frame in theScene Viewand can quickly overwhelm theTLS Allocatorif they contain heavy logic or constant logging.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.