- Home /
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
Answer by YoungDeveloper · May 09, 2015 at 01:09 AM
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.
thank you for your reply.
At first I've tested by using Resources.Load() in the IEnumerator without using any array but somehow the allocated memory keep growing up until memory allocation crash occur.
In order to avoid memory leak I've proceeded like this : myImage.overrideSprites = Resources.Load("path to the sprite");
but even using it this way, the memory keep growing up until memory crash. Something is still keeping in memory but I don't know what is keeping in memory.
Are you sure that's what causing the issues? How much exactly you are loading, how are you storing them, how much and what's the params of the sprites.
Each Sprite have the default setting (mode:single, Pixels per unit:100, $$anonymous$$ax size:1024, format:compress).
I've created a new project with only one image in my canvas (and the default camera) and the memory issue occur even if I only call Resources.Load(); without assigning to my image.
The function Resources.Load() is called several times per second because this part is inside a loop in my IEnumerator which wont stop running until using the StopCoroutine function when a specific event occur.
So basically my code looks like this:
IEnumerator startSpriteAnimation(){
int currentIdx = 0;
while(true){
currentIdx++;
if (currentIdx>1000) currentIdx = 0;
myImage.overrideSprites = Resources.Load<Sprite>("take right Sprite following currentIdx");
yield return null;
}
}
I don't manage the currentIdx this way, it's just to make the code easier to read.
In this IEnumerator, I change my image using 1 of the 1000 sprites for making animation, I also have other IEnumerator which use between 20 and 130 Sprites for the animation of different images.
In my project I've only set the first animation with the 1000 sprites,
If your sprites are animations why don't you group them, also 1k seems quite large for animation frame.
It maybe a stupid question but how can I group sprites? One big spritesheet image containing the full animation?
If so, it means that I have to use animator component? As quite a lot of animator will be needed, I guess the appli will be come a little slow no?
Answer by getwreckedgame · Feb 27, 2017 at 07:29 AM
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: https://pimdewitte.me/2017/02/27/unity-scene-memory-allocation-on-sprite-sheets-with-multiple-object-instances/
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613195103im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why do iOS scales my sprites while they are working fine on Unity Editor and Android? 0 Answers
Xcode > Unity SendMessage 1 Answer
Sprite missing when switch platform to IOS 0 Answers
iOS crash with "Memory pressure" error - too many sprites? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers