Memory Leak Adventures in Editor (3.4, lot of instantiated objects)

Hi Unity Answers.
Here’s the situation:

  • Unity 3.4 Pro on Windows 7 64 bit
  • an Empty Gameobject with a C# script that runs in the Editor, activated via a public bool flag visibile in the inspector
  • a child object: a plane with a texture on it (2048x2048 uncompressed truecolor)
  • a simple sphere object prefab

The purpose of this script is to create, in the Editor, a prefab that contains numerous objects disposed accordingly to a texture channel.

The script:

Loop inside OnDrawGizmos once flag is activated:
.create an empty gameobject (a container for all the instantiated objects)
.foreach pixel in texture, if random(1,1000) == 1, instantiate sphere prefab, child of that gameobject
.if done, do nothing else

The result is a GameObject with actually a thousand of child spheres. Nice, it works flawlessly.

Now, here comes the strange things:
Unity’s RAM usage before the generation: 177 MB
Unity’s RAM usage after the generation: 399 MB
Unity’s RAM usage after manual deleting all the spheres: 405 MB (!?!)

Hmm. Ok, let’s do another cycle:
Unity’s RAM usage before the generation: 177 MB
Unity’s RAM usage after the generation: 473 MB (1480 spheres)
Unity’s RAM usage after manual deleting all the spheres: 475 MB (?!)

Again:
Unity’s RAM usage before the generation: 475 MB
Unity’s RAM usage after the generation: 544 MB (1477 spheres)
Unity’s RAM usage after manual deleting all the spheres: 547 MB (!?)

Someone tells me that Unity or Mono or… do not deallocate memory instantaneously, but only after some “block” memory allocation. Ok, so…

“I can go on.”
“Do…”
(IT Crowd citation)

I’m actually at 950MB of RAM used and it seems not to grow anymore. Maybe this is all caused by the way Windows 7 gives memory to applications.
Or is it really allocated? In part? Hmm, i don’t know.

BUT, this is not the real problem. The real problem is that if i hit the play button to start the game after a generation is done, memory usage grows at exponential speed (in about 4/5 seconds), goes around 1.1 GB and then Unity crashes with the “Too many heap sections” error (Fatal error in GC).
This does not happen if i delete the object that generate spheres (the one with attached script).

Now, i know that using the OnDrawGizmos as a continuos editor Update() could not be really correct.
You’ll say “ok, there’s something in the script that causes memory allocation”.
The script does nothing when it stops to generate. There is a flag that exits from OnDrawGizmos (tested).
The OnDrawGizmos is also enclosed with the #IF UNITY_EDITOR to be sure is not called in game (i think that it’s not called however).
If i DELETE the object that generate all the spheres, everything goes OK and i can play the game scene.

I can provide a test scene if could be useful.

Thanks in advance.

I tryed that script on our main project (with a different object, not the sphere, and with two tousands of objects). When i, from the editor, delete the GameObject that contains all the instanced objects, the memory goes up fast again to 1.4 GB and then the crash.
If i delete the “creator object” before, everything goes fine.