- Home /
Determining Texture Memory Usage
I'm currently trying to port a game to iOS and running into memory issues. I know that the game is pretty liberal with memory usage, and I'd like to track down the biggest offenders so I can clean them up. However, running the profiler produces results that I don't quite understand, and I was hoping somebody could help me shed some light on what they mean.
Here's what I see when I look at the memory usage reported by the profiler on the iOS build from within the editor:
Total: 180.7 MB **Textures: 1127 / 65.3 MB** Meshes: 596 / 0.9MB Materials: 261 / 102.9 KB AnimationClips: 0 / 0 B AudioClips: 16 / 3.4 MB Assets: 2206 GameObjects in Scene: 764 Total Objects in Scene: 4232 Total Object Count: 6438
This claims that I'm using far more memory than I would have expected on textures. However, when I look at the same scene using Resource Checker, what I see is much closer to what I expect:
25 Textures total, whose combined sizes are 14.03 Mb
3 textures that are 2048x2048 RGBA Compressed PVRTC 4 bits, each taking up 2.0Mb
7 textures of various non-power-of-two dimensions. They aren't compressed, but they're also not particularly large; they range in size from 556k to 750k.
5 textures that are 1024x1024 RGBA Compressed PVRTC 4 bits, each taking up 512k
9 remaining textures that are all power-of-two sizes between 4x4 and 512x512. None of these take up more than 256k.
I can't find any objects in my scene that are using a texture that isn't one of these 25. This leaves me with two primary questions:
1) What are the other 1121 textures that the profiler claims that I'm using? Are these textures being used by the editor? If so, how can I get a better sense of where my memory is actually being used on device?
2) The profiler claims that I'm using 180.7 MB total, but the breakdown below only adds up to around 70 MB. Where is the extra 110 MB coming from?
Thanks for any help.
Hi Awall. Did you ever figure this out? Going to be digging into profiling my own app soon and want to be prepared in case I run into a similar issue.
total does i'm pretty positive include the heap (unallocated memory) That is to say basically cause your running on a modern computer unity asked for some memory and your computer handed it 180mbs. It could have handed it less but saw so no need.
The largest issue is i'm fairly positive compressed textures aren't compressed in the default build. they get compressed once you build the exe. As such you are currently working with uncompressed 2048 x 2048 textures. (im not sure the size but you can look it up, basically check if going from 14mb to 70 makes sense IF those textures werent compressed add those values up as uncompressed).
The 1127 I believe just references how many objects are referencing a texture (your only using 16 textures but each texture is being used hundreds of times)
Basically you need to try to reduce the texture size and compile to an exe and maybe try changing the type of texture. (do you need RGBA for example, because RGBA includes an alpha transparency layer, are your textures partially tranparent? if they are fuly visible you should use a format that doesn't include a transparency channel.)
@gdeglin: No, I didn't really figure this out. I eventually lost faith in the profiler after it started giving me readings that were obviously incorrect (for example, as a test, I tried calling Resources.UnloadUnusedAssets() every frame; this caused Total memory usage to gradually drop... and drop... and drop... all the way down to 0.) Ins$$anonymous$$d, I looked at the Editor Log after a build, which shows you the size of all of the resources built into your game; it's not exactly what I was looking for, but it showed me that there were quite a few textures being built into some of my scenes than I'd thought, so I was able to improve things that way.
@sparkzbarca: Thanks for the explanation. As I mentioned above, I've found the profiler to be generally unreliable, but your comment would explain some of the strange data that I was seeing.
Did you ever figure out why it was giving these figures? I'm getting the same issue on my end… overblown textures. Only the compressed ones though, the bitmaps are actually smaller for some reason.
Answer by anasiqbal · Apr 08, 2016 at 07:15 AM
Hi, I know its quite late to reply but incase if anyone else is looking.
Yes, these textures are Unity's textures (some used by the editor).
You can get a better idea of your memory usage by filtering out the textures using
texture.hideFlags
. (source code attached)int totalTextureMemorySize = 0; HideFlags hideFlagMask = HideFlags.HideInInspector | HideFlags.HideAndDontSave; HideFlags hideFlagMask1 = HideFlags.DontSaveInEditor | HideFlags.HideInHierarchy | HideFlags.NotEditable | HideFlags.DontUnloadUnusedAsset; var textures = Resources.FindObjectsOfTypeAll(typeof(Texture)); foreach(Texture t in textures) { if (t.hideFlags == HideFlags.HideAndDontSave || t.hideFlags == hideFlagMask || t.hideFlags == hideFlagMask1) continue; textureCount++; int memoryUsed = Profiler.GetRuntimeMemorySize(t); Debug.Log("Texture: " + t.name + " : hideFlags: " + t.hideFlags.ToString() + ": Memory: " + (memoryUsed/1000000f)); totalTextureMemorySize += memoryUsed; }
Resources.FindObjectsOfTypeAll
helps you find all the textures loaded into the scene. The if
statement helps filter out unity textures, and Debug.Log
gives you information about the textures and their hideFlags.
You can comment the if
statement temporarily to see what textures being loaded by unity and their hideFlags.
NOTE: Profiler.GetRuntimeMemorySize
works in the editor and development builds but not in release builds. And this function is very slow, therefore, it is not recommended to run it every frame. For more information see here and here.
Also, looking at the logs generated by above code You will figure out that when textures are loaded into the scene there sizes are increased. By my experience a 0.5mb PVRTC compressed image when loaded into the scene takes almost 4 mbs of memory (if your looking in the editor). But if you check this on a device (I checked on iPhone4s) the size will be doubled i.e. 1 mb for a 0.5mb PVRTC compressed texture. I don't know why does this happens (still doing some research) but if any on knows why please let everyone know. But at least you will get the idea how much memory is being used by your textures inside a scene.
@awall: Hope you will find some trust in the profiler.
Answer by stevesan · Nov 14, 2013 at 04:51 AM
Interestingly, the "memory" graph part of the profiler typically shows a lower level than the text status. I guess the text includes the total reserved size, not actually used?