Hello Guest

Author Topic: PNG atlases take double memory usage as Uncompressed Unity Texture?  (Read 14970 times)

imkc

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Hi,

I've tried to use PNG atlases to reduce the app size after installed in iOS. I could see the the app size become 55MB from more than 70MB.
But I found the memory usage increased after changed to PNG atlases. I could see in the profiler, The memory usage of "MainCollection" atlas in PNG is 32MB, while it is 16MB in Unity Texture. The screenshots of settings are attached, and "PremulVertexColor" is used for both. Is there anything I missed?
« Last Edit: February 07, 2014, 07:59:38 am by imkc »

neror

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: PNG atlases take twice memory usage as Uncompressed Unity Texture?
« Reply #1 on: February 06, 2014, 04:58:37 pm »
I was having the same issue. I had to switch back to Unity textures because I kept running out of memory on older iOS devices.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: PNG atlases take twice memory usage as Uncompressed Unity Texture?
« Reply #2 on: February 06, 2014, 10:19:09 pm »
png textures use more memory on IOS as the png file will be loaded into memory in addition to the texture itself.  Shouldn't be double though - how are you getting this data?

imkc

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: PNG atlases take double memory usage as Uncompressed Unity Texture?
« Reply #3 on: February 07, 2014, 02:23:12 am »
png textures use more memory on IOS as the png file will be loaded into memory in addition to the texture itself.  Shouldn't be double though - how are you getting this data?

In the Unity(4.3.4)'s Profiler, connected with a device running iOS 7.0.4.
You can find in the attachment 1, which is using Unity Texture. The largest atlas costs 16MB here.
And in attachment 2, which is using PNG atlases, the largest atlas's gone in "Assets->Texture2D" section of profiler.
But we can find it in "Scene Memory" section, which costs 32MB in attachment 3. I am sure it is the same atlas as the one of attachment 1.
And the XCode debugger also reported the similar increasement of memory usage.

« Last Edit: February 07, 2014, 07:59:55 am by imkc »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: PNG atlases take double memory usage as Uncompressed Unity Texture?
« Reply #4 on: February 07, 2014, 11:53:10 am »
Investigated this and it looks like using LoadImage (which the png atlases use) allocates another copy in memory which Unity doesn't seem to free. A 5.3MB 1024x1024 texture takes up 10.7MB, which makes absolutely no sense.
Edit: I missed something in the profiler - It actually does seem to matter if Apply is called. Try adding this line to tk2dSpriteCollectionData.cs to see if it improves things.


            bool assignTextureInst = false;
            if (pngTextures.Length > 0) {
               assignTextureInst = true;
               textureInsts = new Texture2D[pngTextures.Length];
               for (int i = 0; i < pngTextures.Length; ++i) {
                  Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, textureMipMaps);
                  tex.LoadImage(pngTextures.bytes);
                  textureInsts = tex;
                  tex.filterMode = textureFilterMode;
   #if UNITY_EDITOR
                  tex.hideFlags = HideFlags.DontSave;
   #endif
                  tex.Apply(textureMipMaps, true);
               }
            }
« Last Edit: February 07, 2014, 12:05:03 pm by unikronsoftware »

imkc

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: PNG atlases take double memory usage as Uncompressed Unity Texture?
« Reply #5 on: February 07, 2014, 03:41:50 pm »
Investigated this and it looks like using LoadImage (which the png atlases use) allocates another copy in memory which Unity doesn't seem to free. A 5.3MB 1024x1024 texture takes up 10.7MB, which makes absolutely no sense.
Edit: I missed something in the profiler - It actually does seem to matter if Apply is called. Try adding this line to tk2dSpriteCollectionData.cs to see if it improves things.


            bool assignTextureInst = false;
            if (pngTextures.Length > 0) {
               assignTextureInst = true;
               textureInsts = new Texture2D[pngTextures.Length];
               for (int i = 0; i < pngTextures.Length; ++i) {
                  Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, textureMipMaps);
                  tex.LoadImage(pngTextures.bytes);
                  textureInsts = tex;
                  tex.filterMode = textureFilterMode;
   #if UNITY_EDITOR
                  tex.hideFlags = HideFlags.DontSave;
   #endif
                  tex.Apply(textureMipMaps, true);
               }
            }

It works as expected! Thank you so much!

imkc

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: PNG atlases take double memory usage as Uncompressed Unity Texture?
« Reply #6 on: February 07, 2014, 04:42:58 pm »
And I tried to free the memory of PNG files by appending last bold lines below:

bool assignTextureInst = false;
            if (pngTextures.Length > 0) {
               assignTextureInst = true;
               textureInsts = new Texture2D[pngTextures.Length];
               for (int i = 0; i < pngTextures.Length; ++i) {
                  Texture2D tex = new Texture2D(4, 4, TextureFormat.ARGB32, textureMipMaps);
                  tex.LoadImage(pngTextures.bytes);
                  textureInsts = tex;
                  tex.filterMode = textureFilterMode;
   #if UNITY_EDITOR
                  tex.hideFlags = HideFlags.DontSave;
   #endif
                  tex.Apply(textureMipMaps, true);
               }
    #if !UNITY_EDITOR
               pngTextures = new TextAsset[0];
    #endif

            }

It works properly in my case. However,  png atlases cost more memory than Unity Texture in XCode debugger, but the wasted memory should be reserved by mono, which is reported in Unity Profiler as well, and I don't think it does matter in my case ;D

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: PNG atlases take double memory usage as Uncompressed Unity Texture?
« Reply #7 on: February 07, 2014, 04:55:41 pm »
setting pngTextures = null may cause issues if you need to reload the sprite collection later, just something to keep an eye out for.