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:
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?
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?
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.
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?