- Home /
Sprite Atlas is loading multiple times in the memory
I'm having this problem where my sprite altases are being loaded into memory multiple times. This causes unnecessary memory usage and, on some of my devices that run android 6.0 with 1GB of ram, the app crashes. Mind you that i have some android devices with android 6.0 and 1GB that can run the app just fine, but i still have the same issue, very large memory consumption.
I tried profilling with the android studio profiller and with unity build-in profiler and the memory usage reported was the same.
The app that i am developing is using ~1000 sprites, so it makes sense to use sprite atlas, both for build size and memory consumption.
The app crashes right between two scenes, i am using Async load, and at it's peak the memory usage reaches 0.53gb, which in my opinion for a small android build seems insane, right after that the app crashes on that device.
The method i am using for loading the sprite altases is with SpriteAtlas.GetSprites(), than i am storing the sprite in a static array, and i am making sure that SpriteAtlas.GetSprites() is never going to be called during the current game session. This, in my mind, removes any connection between sprite atlas and it's contents. I am using this approach because i've seen that pulling sprite by sprite, using the SpriteAtlas.GetSprite() method, can be quite CPU and memory intensive in comparison with my current method.
Unfortunatley the unity documentation does not give any warning for bad practices so in this case i am unsure if am missing something in my programming, and it gets quite frustrating not knowing how should i approach this problem. Also i've seen that many other developers have this issue, but most of them have this problem in editor, where the profilling data is not coherent when it comes to a fizical device build.
This is really important since i will be publishing more games with the same theme, and i need to improve my tehnique in order for my apps to perform at their best both on high-end devices and low-end.
If there are any questions, about the code i used or about how the sprite altases are packed, i am more that happy to resolve this problem, not just for me but for any other developer that may come across this problem.
Don't have a solution to this problem, apologies, so i'm not posting an answer. I'll be having difficulties in this area co$$anonymous$$g up here soon I'm sure, because i did sorta learn something from this post, haven't looked into Sprite Atlas's yet and by this post, sounds like the route I'll need to take over what i was doing. Glad i read this and will be fav'ing it for further future references. Thanks for posting.
PS.
Whats your take on object pooling for this sort of instance? or is this a premade class for dealing such an aspect?
I am storing the sprites that i know i am going to use them in all scenes in a static array. This way there will not be any hiccups when i will try to fetch any of them.
- Update I found out why the sprite atlases are being loaded multiple times on the build. The reason for that is because some of the sprite altases have more than 1 "packs". For example if i try to pack 48 sprites into a 2048x2048 atlas, and they won't all fit in the atlas, unity will create 2, 3, 4 etc, to fit them all. I guess this is the answer. But unfortunatley this does not solve my problem, the app still crashes, and i am looking for some other reason why is crashing. I will keep you up to date if i found something usefull :)
Answer by ComaneanuMugurel · Feb 19, 2019 at 04:29 PM
This was a something that i overlooked and for some of you that may come across this problem, this is completely normal. The reason behing the atlas being loaded multiple times, is that each atlas has a number of "pages", and each of that page is being loaded into memory. There is no unecessary memory usage. Hope this helped some of you understand what happens, and is a bit sad that i had to find this out by testing not from the documenation.
Thank you for the feedback, I've made a note to add it to the documentation. Also, I'll look if we can't improve upon the UX for this in the new $$anonymous$$emory Profiler Package.
If anybody else encounters similar issues and it's not due to paging: it could also be due to there being multiple versions of the atlas, stem$$anonymous$$g from different asset bundles. Or if you profile the Editor, the editor retains a copy of the atlas on the CPU for read/write access.
How did you deter$$anonymous$$e that these were pages? I think we have the same issues. I'd like to confirm that it is paging for us too.
Answer by HofiOne · Jan 13 at 01:46 PM
just for the record, nowadays unity also prefixes the pages indexed, like
sactx-01- sactx-02- sactx-03- etc.
(also you can see in the references count column different values that suggests those objects are not the same)
Your answer
Follow this Question
Related Questions
ShaderGraph Position from Texture Sample Issue 0 Answers
Sprite/ Atlas/ Memory limit in 2D Mobile Games? 2 Answers
Sprite2D texture memory optimization 0 Answers
Mobile transparency issue 0 Answers
Does Effective Usage of SpriteAtlases Double Storage Used? 0 Answers