Texture2d.LoadImage uses tons of memory

Hi

I’m using Unity3 Pro and loading 3 1200x1600 PNG textures by Texture2D.LoadImage() and each time I do the memory uses increases by about 400MB! I’ve tried also using the WWW class but it does the same thing.

When I stop the Unity game the memory is returned. There’s no way loading 3 textures should eat up 400MB of memory…I’m even loading them without mipmaps…

Really looks like there is a bug here.

Any ideas?
-Andrew

Bump Unity guys I think there is a bug here… I can supply a sample scene if requested

I’m sorry but why didn’t you filed a bug with repro scene? Go ahead and drop me the case number

ahh now i see how to file a bug :slight_smile: thanks I’ll do that

Case 378418 :slight_smile:

Reported this already with beta 4 on the iPhone side (I loaded 12 textures a 480x320 through WWW on an otherwise empty scene and it crashed an iTouch 3GS due to running out of memory), seems like it was not solved yet then. case 370039

LoadImage: results are expected:
first of all - you create texture from script - so we must keep copy of it on unity side (so you can access it from script).
second - your texture is non-square - so apart from memory to just load it - we need additional buffers to “squarify” it.
third - the memory shown is not unity-only memory but also memory used by opengl/directx. So their internal representation goes there too.

So, the easiest way to test i do not bullshit you - make the texture square: for me it gave nearly +10 mbs as expected (1024x1024 counted twice on unity side and inside driver plus some overhead in between ;-))

Thanks for explaining Alexey. It still sounds like a lot of memory… Let me step through the numbers here so I can better see where the memory is going.

My texture is 12001600ARGB. That’s 7.68MB. Each time I load the texture my memory footprint increases by ~74MB. This is why I was alarmed, it’s using 10 times the memory of the texture :slight_smile:

You say it needs to be squarified and have an extra copy in Unity and a copy in the drivers.

So 16001600ARGB = 10.2MB

10.2MB (driver) + 10.2MB (unity) = 22.4MB… I must be missing something here.

Is the internal representation stored in floating point format? Coz that would bring it to 40MB which would be closer to 74MB.

I’m interested in understanding where the memory is going :slight_smile:

Thanks Alexey! Btw, I am also in SPB :slight_smile:

Interestingly I seem to have found a workaround. In my case I only need to load 8 textures from disk at a time, so I just create the textures in the Unity editor at the same resolution they are on the disk and do a LoadImage() into them. It seems to use a lot less memory.

Squarified is not so easy as you imagine
First we need to create texture to give to driver - its extensions should be power-of-two - so for you it is 2056x2056. Also in case you need mipmaps we need exactly square power-of-two (in prev case you can get away with rect texture) and as a plus we need to allocate memory for mip levels themselves (think of it as plus 2/3 of original memory)
So we have 7.5 mb + (1+2/3)*16 mb +16mb + 16 mb (in driver but noone knows exactly how much ;-)) = 66 mb.
And this goes only for memory used by texture.
Sure we can do better, but you have to live with that for now
P.S. the workaround sounds good