crash memory allocation with Resources.Load()

Hello everyone,

I’ve searched for hours on the internet and on the unity3d forum, a lot of thread as been created for almost the same error as mine but I still don’t know how to fix my trouble. (target platform is ios, using Unity 4.6.3f1)

here is my trouble:

in my canvas I have a simple Image with a sprite by default.

The purpose of this image is to simulate an animation by changing the image.sprite or image.overrideSprite which will be done in an IEnumerator.

The problem is that I have 1000 sprites for this animation and on my Mac mini after displaying the 200 sprites, Unity3d crash with the memory allocation error.

So far here are what I’ve seen on the internet and tried without any success:

1- Create a Sprite list using Sprite → if Sprite is made public Unity crash after putting around 300 Sprites, If Sprite is private, using Resources.LoadAll(); will quickly result to memory allocation crash

2- tried to Resources.UnloadAsset() : by putting image.sprite as parameter result in a cast error next time image.sprite is called, if I create a Sprite (Sprite mySprite; mySprite = Resources.Load(…); image.sprite = mySprite) and put this Sprite in UnloadAsset(), I’ll still have the memory crash.

3- create a prefab of my image and use Instanciate(): I used DestroyImmediate() for removing my gameobject a recreate a new one. → still have the memory crash.

my question is : in an IEnumerator, how can I change the image.sprite/image.overrideSprite using Resources.Load() without having memory leak?

I’ve also tried this without any result:

Sprite mySprite = Resources.Load();
myImage.sprite = mySprite;
mySprite = null;

could you please help me with this trouble? I have to get this works before monday:S

I really thank you in advance for your help

Scene gameobject components are loaded into memory, if you have 3k array, it means it will be loaded in memory even if you aren’t using it. If you need certain items in certain time you could simply load only what you need using Resources.Load.

Hey!

I just ran into the same problem, and it turned out a sprite sheet that had a lot of instances in the scene resulted in a large amount of memory being allocated. I wrote a blog post about it here:

Basically if you use sprite sheets, unity will allocate SPRITE_SHEET_SIIZE * INSTANCES_OF_CUMULATIVE_OBJECTS_IN_SCENE in memory. This can lead to insane amounts of memory allocations, especially for lower end devices this can be bad. Figured it out a few minutes ago after a week of debugging, so hoping this post saves someone time.

  • Pim