I’m trying to troubleshoot an issue where memory is not freed up when I stop and play my application within the editor:
Have task manager open
Hit “Play” in the editor
Memory jumps up as expected (say from ~250MBs to ~1300MB)
Hit “Stop” in the editor
Memory stays where it was
Hit “Play” in the editor
Memory jumps up unexpectedly past it’s previous point (say from ~1300MBs to ~2275MB)
What could cause this? Doesn’t unity throw out all the managed stuff when you stop playback and reset it back to it’s development state? It does not deallocate step 3’s memory no matter how long I wait
I have it isolated to this code, yes, 8 million particles is a bit nuts, bear with me:
However, running this code in isolation on a fresh project does not reproduce the issue. I’m at a loss, what could be forcing this memory to be sticking around allocated?
I can verify that the monobehaviour (and it’s container game object) is destroyed when I stop the application.
Adding this code to the problematic class appears to help. The memory still stacks up, but now it never goes about 2.5GB even after multiple start-stops
public void OnDestroy()
{
ForceCleanupAndDestroy();
}
private void ForceCleanupAndDestroy()
{
if (particleBuffers != null)
{
for (int i = 0; i < particleBuffers.Count; i++)
{
ParticleSystem.Particle[] buffer = particleBuffers[i];
if (particleBuffers[i] != null)
{
Array.Resize(ref buffer, 0);
particleBuffers[i] = null;
}
}
particleBuffers = null;
}
if (gameObject != null) { Destroy(gameObject); }
}
It happen to me all the time in different version. Editor eat memory (more of less) by non stop. In the end I should restart editor because it take all 14 gb ozu. Profiler is useless, It never show the reason all of this.
@Khafeine and @id0 , this topic is rather old and likely unrelated to your issues.
Generally, Unity allocates memory in pools and while the usage of these pools may vary, it never or hardly ever deallocates them. This goes for native and managed memory (managed heap sections might get deallocated if they are entirely empty, but usually you’ll have had some random allocationnin there that you still hold a reference too, so it won’t be freed. Check the Memory Map for this). The reason it grows can be manifold, the reasons the usage of the pools doesn’t go down between entering play mode as well. To identify what particular issue you are hitting you might first check memory usage in the Profiler windows memory module, observing trends to see what category it might belong to.
Also, the memory Profiler linked to in this thread has since been discontinued and a new one available as a package replaces it. Among other things, it allows you to see fragmentation of the pools and diffing between snapshots. Both these features can help track down the issue and, if it’s origin is not within your project, be used to file a qualified bug report so that the issue can be swiftly fixed.
If something in there is not clear, confusing, frustrating or otherwise irking you, I’d be very happy to hear your questions and/or feedback in the Profiler Previews Subforum.