Guys, trying to profile a memory leak in Editor;
I’ve made a sample inside of profiler, please have a look:
Need to know what particular thing is referencing my TempBuffers (or what created it), or who created it in the first place? Perhaps there is an evil asset / unitypackage which did it? We have like 1400 render textures generated in Editor.
Thank you!
I’m experiencing this same issue only on the scale of over 100,000 TempBuffers. Did you ever figure out what was causing this?
I did notice it happened when Collab (Services panel was opened); But I am not sure… Perhaps it’s a problem with something else, but they might have forgotten to release texture memory or smth, generating new texture every frame
Sorry for reviving this old thread, but still experiencing this issue in Unity 5.6. For me it seems to originate somewhere from the scene view window and the terrains in my game. In play mode with the game window maximized I do only see some of these TempBuffer render textures being created, but they also seem to be released at some point again.
If I have the scene view visible and start doing stuff like filtering the hierarchy to look for certain Game Objects, I notice those TempBuffer render textures being created en masse and they will persist until I switch projects / close Unity.
Depending on what I do these can crash Unity or even my entire PC quickly.
For now I made me a button in Unity that executes the following script to release the RTs again during design time, maybe it is useful for someone:
public void ReleaseRT()
{
var rendTex = (RenderTexture[])Resources.FindObjectsOfTypeAll(typeof(RenderTexture));
for (int i = 0; i < rendTex.Length; i++)
{
if (rendTex[i].name.StartsWith("TempBuffer"))
{
RenderTexture.ReleaseTemporary(rendTex[i]);
}
}
System.GC.Collect();
}
1 Like
Thanks! I’ve also improved your script, so it does it automatically
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
// occasionally releases TempBuffers, which are created through a bug, beloning to Unity.
// Without it, unity can consume a lot of memory, have bad fps in editor & even crash.
//
// Authors: OneManBandGames, Igor Aherne
// https://discussions.unity.com/t/669090/4
[InitializeOnLoad]
public static class AutoReleaseUnityRenderTargets{
#if UNITY_EDITOR
static double _flushIntervals = 90;//every 90 seconds, or tweak to do less often (involves garbage collection)
static double _lastTime = 0;
//ran via [InitializeOnLoad]
static AutoReleaseUnityRenderTargets() {
EditorApplication.update -= OnEditorUpdate;
EditorApplication.update += OnEditorUpdate;
}
[UnityEditor.Callbacks.DidReloadScripts]
public static void OnCompiled() {
EditorApplication.update -= OnEditorUpdate;
EditorApplication.update += OnEditorUpdate;
}
static void OnEditorUpdate() {
if(EditorApplication.timeSinceStartup < _lastTime + _flushIntervals) {
return;
}
_lastTime = EditorApplication.timeSinceStartup;
//release the bugged textures:
var rendTex = (RenderTexture[])Resources.FindObjectsOfTypeAll(typeof(RenderTexture));
for (int i = 0; i < rendTex.Length; i++)
{
if (rendTex[i].name.StartsWith("TempBuffer"))
{
RenderTexture.ReleaseTemporary(rendTex[i]);
}
}
System.GC.Collect();
}
#endif
}
1 Like
Thanks, excellent idea! Replaced my shady button with your improved version. 
For future Google searches ending up here (same as me with the same issue):
Most likely this issue is caused by calling RenderTexture.GetTemporary without a matching RenderTexture.ReleaseTemporary. Either from your code or from a rogue package. You’ll find out by opening visual studio and exploring all found calls to RenderTexture.GetTemporary.
Took me a while to figure this out - while most parts of Unity are fully managed (meaning you don’t need to think about releasing resources) this one isn’t.
2 Likes
Thanks for posting the solution for this! I searched across my project and I found some calls from all kinds of packages & Unity Standard Assets and I from first glance it looked like all would call ReleaseTemporary at some point, so not sure what caused my issue. Then again I’m not sure if it even is present in my project anymore, I did not experience the issue for quite some time now even with the script workaround from above disabled.
I could imagine that it was a bug in some of the packages that caused it, and it “updated itself out” in the meantime.
The good thing is with your info I’m confident that I will be able to track it down now if it hits me again, so thanks again for the update!
1 Like
You might be able do this with either of these free tools:
Heap Explorer
https://discussions.unity.com/t/699121
Memory Profiler
https://bitbucket.org/Unity-Technologies/memoryprofiler
Both tools allow to inspect which objects hold a reference to a particular other object. Here is how you would try to find this in Heap Explorer:
-
In Heap Explorer, click “Capture & Analyze”
-
Open the “C++ Objects” view
-
Find the leaking render textures
-
Select one of these RenderTexture objects
-
It now should display a list of other objects that reference it
2 Likes