- Home /
Textures/Atlases and memory management
I've been making a game for 5 months now, following what the profiler tells me, but I've been told that the unity profiler is kind of unaccurate, but I never thought it would be so much.
I've just started using Instruments for iOS andI've discovered that (on iOS at least) the profiler is completely irrelevant, or that I'm doing something very wrong, I'm asking you this to make sure I'm not doing something wrong.
The game we're making is really texture heavy (we have over 30 jam packed atlases, most of 'em 1024x1024), at the title screen, the unity profiler tells me it uses about 20MB, the Instruments profiler tells me 90MB. What I understand from that, is that the unity profiler hides the textures that are currently used but not on screen from the memory profiler.
This means that the unity profiler tells me that only the title screen is in memory and my other textures are somehow compressed, ready to be used, but in fact my whole UI is loaded with all it's textures fully uncompressed.
Now that is some hope of not having to heavily manage those textures gone, but I'm telling myself that there must be a reason why the profiler tells me that they are automatically managed?
So is there some magic incantation to make in order to make this automatic textures management work, or do I have to manually manage those?
The answer is: It depends.
How are you loading the textures? AssetBundles, WWW class (no AssetBundles), Resource directory or are they part of a scene?
I guess this is the reason you require that much memory. Unity loads the complete scene to memory. If you want to prevent this you have to load the assets on the fly.
Put them in a folder and load them via WWW class or the Resource class. $$anonymous$$eep in $$anonymous$$d that you have to dispose textures manually if they are no more needed (Object.Destroy()) otherwise the memory is not released.
Sometimes it helps to call Resources.UnloadUnusedAssets to free the memory.
Thanks for confir$$anonymous$$g this, it is just that I feel betrayed by the unity profiler who told me that even when I have many atlases used in my scene, only the ones I currently see are "uncompressed" or use space in RA$$anonymous$$, so there is really no trouble in not managing memory, but I guess I'll have to rethink my strategy.
You're welcome. In the last years I also had to learn that you should never rely only on the profiler or the memory functions unity offers. $$anonymous$$any memory issues are just not covered.
Some tipps on memory usage:
Always have a task manager or other memory tool running
Write code in an unmanged style (clean up your instances properly, null object, remove event handlers, ...)
Restart the editor if you think some memory corruption occured
Free static objects on you own or they might remain in memory when loading an other scene or leave the play mode
Do not load to many resources at the same time to prevent fragmentation
Your answer
Follow this Question
Related Questions
Determining Texture Memory Usage 2 Answers
TEXTURE has wrong size in memory 1 Answer
Why are compressed textures showing up huge in memory? 0 Answers
Changing textures accumulate in memory. iOS. 0 Answers
SpriteAtlas clone memory problem 0 Answers