- Home /
Heap problem when creating a large number of objects.
Hi.
I ran into this problem yesterday when I was creating a large number of objects in the scene at editor time.
Here's what I do:
I have an edit time class and a MenuItem that performs an time consuming operation that involves creating objects in the scene.
I have a loop. I read about 200,000 records (one at a time) from a web service and I generate objects for them once at a time.
Just about when I reach 18114 number of records processed, the unity hangs, and the memory consumption increases drastically and unity starts to use full cpu power (13% in a quad core hyperthreaded - which means using a thread up to maximum).
After a couple of minutes I get the heap error message.
Some notes: - I'm using EditorUtility.DisplayCancelableProgressBar to show the progress. - At each iteration of the loop I create an object either having a linerenderer attached to it or a shape consisting of 20 to 40 triangles (textured).
Things I tried: - Bringing the object and type declarations out of the loop. ex:
Vector3[] test;
foreach (var something in aList)
{
test=retreiveTheListUsingWebService(something);
GenerateShapeOrLineRenderer(test);
}
Calling System.GC.Collect after making the possible arrays=null within the loop.
So far, I cannot figure out what the problem is. I have a theory Unity might stack up the GameObject creation commands and waits until the loop ends.
Please if anyone could help me with this I would really appreciate it.
Thank you.
Hmmm, interesting problem - are you sure that there's not something wrong with the code to generate the object that is being triggered by a particular kind of record? Perhaps you could post the code for GenerateShapeOrLineRenderer?
I don't think it's wrong. It works for smaller numbers perfectly and all objects are generated and not even a single error is thrown. It only happens with large number of records. (I've tried 65,000 and it's as smooth as butter)
Answer by Aram-Azhari · May 15, 2013 at 10:33 AM
I finally figured my problem out. As whydoidoit suggested, the internal code was the problem. I traced the problematic record and ran the code within visual studio without involving Unity, I realized it was going into an infinite loop because of some duplicate data.