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.

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.