Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ludo_ics · May 09, 2015 at 01:07 AM · iosspritememory leak

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ludo_ics · May 09, 2015 at 06:14 AM 0
Share

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.

avatar image YoungDeveloper · May 09, 2015 at 08:43 AM 0
Share

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.

avatar image ludo_ics · May 09, 2015 at 09:33 AM 0
Share

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,

avatar image YoungDeveloper · May 09, 2015 at 11:33 AM 0
Share

If your sprites are animations why don't you group them, also 1k seems quite large for animation frame.

avatar image ludo_ics · May 09, 2015 at 11:39 AM 0
Share

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?

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges