- Home /
tools/techniques to find memory-leak roots in unity?
I'm running into memory-leak issues in a game project and having trouble identifying the cause[s].
In other garbage-collected platforms that I've worked on, memory-profiling tools can display a graph, so you can see the 'root' objects that are pinning other objects into memory.
Is there any way to view that type of graph/automate finding root memory objects in Unity?
In terms of env/platform, I'm using unity 4.5.2 and MonoDevelop as the IDE. The target platform is iOS, but the memory issue presents in the Unity editor as well.
Additional detail: there seems to be a variety of objects incorrectly pinned in memory, but the big-ticket problems are textures associated with Resource-loaded prefabs.
Not having a profiler that identifies memory roots, I've been doing the following:
1) For all scene transitions, I have a bumper scene that calls Resource.UnloadAllUnusedAssets and waits for that to complete. Once resource unload completes, it also calls GC.Collect before proceeding with the scene change.
2) I've checked for static properties/singletons are not holding references to zombie objects or assets and haven't found anything there 3) I've checked for anything that survives a scene load and is registered to any event and haven't found anything there 4) (in Unity Editor) I've tried forcing my bumper scene to never load the next scene, and then deleted all objects off the scene graph/hierarchy and triggered additional calls to Resources.UnloadAllUnusedAssets
...so far no luck.
Any help would be greatly appreciated, especially if it's "here's how you can view a memory graph for Unity"
Thanks, Larry
Did you tryied to search about performance tracker? Unity have a tool for you to check the CPU, $$anonymous$$emory, Disk work and other things to help you get performance with your project
@stormzin,
I can see in the unity profiler what's stuck in memory. It also gives you a reference count. What's missing is a memory graph. Without a memory graph, it seems to me like memory leaks are a needle-in-a-haystack type problem.
I couldn't find a way to get a memory graph for Unity but I was able to fix the memory leak. The cause seems to have been related to my having a prefab assigned to an FsmGameObject variable in a Play$$anonymous$$aker fsm that was loaded as a resource.
I'm not sure why that would cause leak behavior, but taking that prefab away from playmaker seems to have fixed everything.
Anyone ever run into odd memory-leak issues with Play$$anonymous$$aker?
I've never run into memory leak issues with Play$$anonymous$$aker. How'd you manage to set a prefab as a game object variable in playmaker? I would assume it would have to instantiate the prefab? or is it a prefab instance? I'd recommend avoiding playmaker for anything but simple statemachines and prototyping.
Answer by smoggach · Sep 16, 2014 at 12:05 AM
Here are some tips:
Resources.UnloadUnusedAssets will hunt down any assets still in memory that aren't referenced by anything and unload them. The key thing here is that they must not be referenced. You shouldn't call this unless you're gearing up to load a lot of stuff and don't require a decent framerate or user input for the next few frames. Unity will do a nice job of garbage collection on it's own as long as you remember to set things to null when you're done with them.
As for unity's profiler: Click on the memory section, click where it says "simple" down where the information is displayed and change it to "detailed". Then click "take snapshot".
This gives you a snapshot of everything loaded into memory at that particular frame and the things that reference it. From here you can look at each one and make sure you know why it's there, or go through really fast and look for things with lots of references.
You will have a much easier time of finding leaks if you name everything that you create at runtime (so it's easier to distinguish in the profiler).
Using a Sprite as an example:
Answer by Saddamjit_Singh · May 03, 2016 at 01:54 PM
Here is what you can do-
1.Check for foreach and while loops in update. 2.Use memory section of profiler.If leak is coming on phone you can run profiler for it also. 3.Set compression of textures to 16 bit if there are many textures in your game. 4.Use System.GC.Collect(). Use it with care because it can cause frequent hangs in game. 5.Use Resources.UnloadUnusedAssets(). 6.Use instruments in xCode.