Hello Guest

Author Topic: Large memory spike when loading PNG atlases  (Read 5241 times)

JJC1138

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Large memory spike when loading PNG atlases
« on: November 30, 2014, 11:27:31 pm »
Updated: As I mentioned below, I had a bug that makes me think I was misinterpreting what I was seeing and probably invalidates this, so please ignore this for now.

Hi there. When I'm using PNG atlases I'm seeing a very large momentary memory spike when my atlases are loading (please see attached screenshot), which unfortunately is enough to exhaust memory and crash some iOS devices. When I use ordinary uncompressed Unity textures I get a much smaller spike.

Is that expected, and is there anything I can do about it? I was thinking it would be due to the data being shuffled from native to managed and back to native again, and I'm considering writing a Unity plugin that would load the textures using native code instead. Do you think that would help with the memory issue?
« Last Edit: December 02, 2014, 11:27:10 pm by JJC1138 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Large memory spike when loading PNG atlases
« Reply #1 on: December 02, 2014, 09:32:47 am »
I suspect its at the point where the texture is created, copied into the GL texture. Not sure what you can do to work around this and a native plugin will probably not change anything at all, unless you kept the texture pngs outside Unity entirely - you could write a native plugin that loaded a png from the filesystem, load it into a GLES texture, then use Texture2D.CreateExternalTexture to create something unity can use.

Also make sure you're not creating mipmaps, that could increase mem overhead temporarily.

JJC1138

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Large memory spike when loading PNG atlases
« Reply #2 on: December 02, 2014, 12:58:38 pm »
Thanks very much for the reply. I saw an older thread about PNG atlases taking up double memory which you fixed by adding the call to Texture2D.Apply to make Unity discard its uncompressed copy of the texture from memory. I suspect that doesn't take effect until the end of the frame, and unfortunately I'm loading three large atlases on the same frame, so their unnecessary uncompressed copies are all in memory at the same time.

Perhaps I could do something fancy to make them load on consecutive frames so that only one of the duplicate copies is in memory at a time, and avoid crashing that way. Otherwise the native idea I had in mind is how you described it using Texture2D.CreateExternalTexture. I found a post on the Unity forum from someone who was doing something similar. Obviously that might get a bit messy, but I'll let you know how it works out if I do ending trying that option.

Thanks again!

JJC1138

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Large memory spike when loading PNG atlases
« Reply #3 on: December 02, 2014, 11:24:49 pm »
I just realized that I had a bug that was causing an extra sprite collection to be loaded inappropriately, so that might actually be the source of my trouble, and I'm no longer seeing that distinctive memory spike when loading levels. I'm still investigating, but please take everything I said earlier with a big grain of salt now. Sorry!