UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

optimization

[Editor] Fix ALLOC_TEMP_TLS Memory Leaks and Slow Play Mode

Solution

optimizationdebuggingeditoreditor scriptingperformancememory management

Unity 2019.x - Unity 6.3.x

Published 25 days ago

Issue

 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.

Explanation

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.

  1. Open the Console and look for the first instance of a StackOverflowException to identify the loop origin.
  2. Locate your script responsible for the recursion and implement a recursion guard or an exit condition.
  3. Eliminate or gate Debug.Log calls that occur inside high-frequency loops or recursive methods to prevent TLS Allocator overflows.
  4. Reset modified ScriptableObject or GameObject fields in the Inspector to their default states to break circular references.

Additional Tips:

  • Check for scripts that run in the editor using the [ExecuteAlways] attribute, as these can leak TLS Allocator memory even when not in Play Mode.
  • If the issue persists for debugging, use the -tls-allocator-temp-size=number command-line argument to increase the default buffer size.
  • Review OnDrawGizmos implementations; they execute every frame in the Scene View and can quickly overwhelm the TLS Allocator if 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.