Bad memory leak in the unity editor

The script bellow will cause the Unity editor to keep allocating memory each time I press the play button.

var elementsToAllocate : int = 10000; 
var dimensions : int = 1000; 

private var done : boolean = false; 
private var testArray : Array; 

function Start() 
{ 
   testArray = new Array(); 
    
   for(var d : int = 0; d < dimensions; d++) 
   { 
      for(var i : int = 0; i < elementsToAllocate; i++) 
      { 
         testArray.Push(Vector3(GetRandomFloat(), GetRandomFloat(), GetRandomFloat() ) ); 
      } 
   } 
    
   done = true; 
} 

function GetRandomFloat() : float 
{ 
   return Random.Range(Mathf.NegativeInfinity, Mathf.Infinity);    
} 

function OnGUI() 
{ 
   if(!done) 
      return; 
          
   GUI.Label(Rect(0,0,100,30), "Done"); 
    
   if(GUI.Button(Rect(100,0,100,30), "Clear Memory")) 
   { 
      testArray.Clear(); 
      testArray = null; 
       
      System.GC.Collect(); 
   } 
} 

function OnApplicationQuit() 
{ 
   if(testArray) 
   { 
      testArray.Clear(); 
      testArray = null; 
   } 
    
   System.GC.Collect(); 
}

This seems wrong to me, if I keep running this test I’ll eventually run out of ram…

Am I doing something wrong here, did I find a bug I should report?

Am I reading that correctly - you need 10 million Vector3s? :shock:

Can’t really answer your question and don’t see anything other than the array size that’s funky in the script. Have you tested it in a build? Sounds like you’re talking about the editor’s RAM usage and I don’t think System.GC will invoke the editor’s garbage collection. Maybe that large of an array is causing probs?

No, this is a small code sample I wrote to easily illustrate the issue without too much code or explanation. The size of the array is to make the “leak” more obvious.

What I’m really trying to do is to parse a file I get through WWW.data, data is basically a string containing the data inside the file, so I split the string using the new line as the separator and store the substrings in an array, then read in position by position in the array a lot like what you would do with file IO GetLine. The problem is that this process uses up a lot of memory and Unity or the GC (not sure who’s responsable for this memory) never seems to release it.

So basically every time I hit the play button the Unity editor allocates more and more memory to the point of using gigs of RAM.

I can’t pretend I know how the editor handles GC. If I have to guess it releases the memory but it’s still allocated to the editor. Then when you define a new array it allocates more to compensate and this should be on the heap since it’s the Array class. Used RAM might not be going up as much but allocated RAM is still available to Unity vs released to the OS because the heap allocation is going for most known. You could check used/allocated via script but still I don’t think you can compare the editor’s usage to a build. Anyway, I’d bet it’s intended because Unity doesn’t want to guess what you want to do next (and who’s ever going to use 10 mil V3s?!). Either way filing a bug never hurts.

And if I did my math correctly, 10 million Vector3s is around 115Mb of data not including any overhead (10mm * 32 * 3 bits). If the above allocation assumption is correct, getting over a Gb would happen fairly fast.

In the end tho, I know not and could be way off! I’m also studying Unity’s memory management and still trying to get my head around all of it. Would love to hear from folks in the know myself.

have you tested the sample script on the webplayer too. I found that a main different of the memory allocating/Caching for WWW class - The standalone (as of editor) and Webplayer don’t have the same behave. Problem in editor maybe just fine on the built later.

good luck.
antonio

When I run a published build I get the memory back when I quit, still it doesn’t release any memory when I call clear when running it.

But aside from that the real issue here is why does the Unity editor keep eating up memory? why doesn’t it use the memory that should be marked as free on the heap after I call clear? Why keep allocating memory until the system runs out of it and possibly crash?

Am I not reading correctly the information in the Activity Monitor(I’m using mac)?

See this thread:
http://forum.unity3d.com/viewtopic.php?t=30322

Might be related. Yours is an extreme case but I’d report it anyway.

Exactly what I’m experiencing, I filed a bug report.

I seem to be having the same problem with an installation piece I’ve created. The Unity player seems to keep using more and more memory, to the point that the program crashes.

I suspect it is texture-related, as I am creating textures with each new instance of a prefab. I destroy them after a while, but I suspect the memory is not being released.

Any resolution of this issue?

Are you storing the textures in an array? if so Clear() will not release the memory for you, you need to loop the array and Destroy each texture before you do Clear()

No solution or updates to my issue as of yet, but the bug is reported so I hope they get around to fixing it or letting me know what I’m doing wrong at some point.

Ratamorph: I’m not exactly sure what was causing the original problem, but chopping long strings can certainly allocate more memory than you expect, depending on how you do it.

In the case you mention (line input from text received by WWW) there is another option that might work for you. The System.IO.StringReader class lets you treat a long string as if it were a text file, so you get a ReadLine function, etc.