another thread on Resource.Load and Unity Memory handling...

Hi all,
I’m having some problems with memory usage on my unity project.

I’m loading something like 40MB textures with Resource.Load from the resource folder but looking at the task manager the unity process grows from ~150MB up to 3,5GB!

I then tried to load a single bunch of textures (10 textures, ~3-5MB) and the process grows up of ~60MB.

I’m simply calling

textures = (Texture2D)Resources.Load(name);
in a loop (name is built in the loop, always different following some specific rules).
I don’t need to destroy them so I think is useless for me to use Instantiate.
any thoughts?
Before saying “use assets bundle” I would just love to understand why so much memory get allocated, I could use the Resource.Load in the future and I don’t want to fight again with the same problem.
Also if anyone has finally found a way to tell unity to discard unused resources from memory it would be just great.
Thank you

40mb by your believe or by the information offered in the unity editor which shows how much it will realy use at runtime? The filesize on disk has 0 importance at runtime, its either a bmp sized format or dxt hardware compressed, but never anything else.
Also did you ensure to not select “GetPixels / SetPixels” unless needed, otherwise it will use RAM and VRAM

Sounds fine depending on the setup you have on the textures.

40MB was the file size on disk.
I ran several tests using the profiler loading 1/10/20/40 textures and what I have is that for every single png 640x360 RGBA compressed DXT5, 53KB on disk (unity inspector tells me is 225KB I don’t know why) the total allocated memory in the profiler grows up of 5,7MB.

About the texture size, from the manual:

Non power of two texture sizes work best when used on GUI Textures, however if used on anything else they will be converted to an uncompressed RGBA 32 bit format

Now if you want to store a 640x360 RGBA 32bit UNCOMPRESSED in memory it should be something like 640x360x4byte = 900KB, I could understand an overhead of 100KB for Texture2D so let’s say 1MB per texture instead of the compressed 53BK but what about the other 4,7MB?

In my project I’d load about 500 textures but that means using 5,7MB500= ~2,8GB memory against 53KB500 = ~26MB…

yes sure, my data should be static that’s why I’m not worried about destroying :slight_smile:

great thanks, I’ll try to use that

EDIT:
forgot to say, i don’t know if it could help, all my textures have the “generate mip maps” and isReadable flags both off

just run some tests with power of two textures.
in all the cases for a 512x256 RGBA DTX5, file size on disk 34KB (inspector says 128KB) looking at the profile the total allocated memory grows up of 410KB for each texture added.

here the memory value should 512x256 = 128KB (the value shown on the inspector?) with an uncompressed of 128KB*4 = 512KB per texture.

edit:
yeah the inspector basically shows up the memory that “should be used” by the texture (in the case a DXT5 texture it should use 1Byte per pixel)
also in my previous post the 225KB shown is basically 640x360/1024

but there’s still a lot of overhead in both cases (power of two or not)…

your calcs omit the fact that you for 99.9999% didn’t disable mipmaps.
mipmaps add a factor of 1.x to the size of the whole thing, which which will explain some differences.

and pow2 is no technical requirement for dxt, just multiply 4 (pvrtc on ogl es devices is pow2 square), just to mention that

well no, actually I did disable the mipmap on all of them… this is way i do not explain that overhead…
any other thoughts for the 0,0001%? :frowning:

yes i know but is always better to have a pow2 texture so you won’t use 4 times the memory (with dxt5 is just 1 byte per pixel) :slight_smile:

tested it against something own …

You are right, at least on WWW.texture and Texture2D.LoadImage there is a massive problem.

I loaded 14 480x320 textures through WWW
Stored them on the storage in the cache folder
Loaded them from there through

Texture2D image = new Texture2D(480,320);
image.LoadTexture(...)

and the result of this nothing is a hit of 200mb on Unity iPhone 3 Pro according the profiler.

I know I didn’t have that on iphone 1.7 and the last time I tested.
This issue is hard to overcome as the whole project bases on dynamically downloaded images (its a digital museum support app that downloads data on the fly for the different things … the json download etc all works without the slightest problem)

On my iTouch 3GS it plain simply crashes and that although its running with 160mb free before start

I’m sorry that you’ve this problem too but is just great to know that I’m not the only one with this issue and is a general unity problem.

should we report it to some forum administrator or what?

btw I’ve made all my tests using unity 2.6.1 on windows here

found another problem too…
also without playing the scene unity can run out of memory.

if you keep the task manager opened and select a texture from you project view unity will load that texture to show it in the inspector and you will see a memory increase for the unity process…
well now if you try to select any other file in the project view you can notice that unity is not discarding that texture but is keeping it in memory!

in my project i have something like 600+ texture and i run out of memory like every 10 min -.-

there’s any way to tell the editor to discard these “temporary assets”?

I tried calling System.GC.Collect() (with and without parameters) in an editor script but it doesn’t do anything

cheers

DestroyImmediate in the script you created them (destroy does not work in the editor and play mode)

I’m not creating them, I’m simply selecting textures from the “project view”…

Btw how can I report this thread to the developers?

it would be just nice to have an option somewhere in unity “do not keep textures in memory for future use” or “clear unity cache” or something like that…

unity has this option

not marking getpixel - setpixel ensures that the textures goes from disk to vram without ram replica for example.

but any removal beyond that at runtime is up to you.
With Unity 3 there is also a stronger “clean stupid user errors” manager behind the scene that will cleanup resources that are no longer used (you don’t hold refernces to it anymore in your unity code)

maybe is because of my bad english, I’ll try to explain myself in a better way.

assumption: I’m speaking only about the editor, no scripts/game running.

It doesn’t matter if my texture has the “isReadable” flag on or off, when I select it in the “project view” the texture get loaded inside the RAM and is shown in the inspector (ok until here…);
when I deselect it (selecting any other assets in the project view or an object in the hierarchy) that RAM doesn’t get discarded, unity hold that memory (maybe to speed up future references).

if you open unity and try to do the above you can easily check the problem with the task manager.

for every 50k texture that I select I my process grows up of ~5MB…

Just to note, you are welcome to report a bug to Unity at any time (menu: Help > Report A Problem).

Well the editor is a different thing.
At least by Unity 2.x its trivial: most of your project will float aroudn the RAM commonly.

this is one of the main problems why larger games kill unity on build: it just exceeds the available RAM, either your physical or the <= 4GB a 32bit application can use at max, cause even in case of build, it does not unload all loaded assets which is horrible

yes that’s exactly the problem.
basically they’re cutting out any “bigger” project from unity…

Thank you andeeee, reporting the problem right now

I was considering doing experiments with the command line build possibility.
as that never starts the visual end it should theoretically get around it.

but so far I had no project requiring it, my 6GB RAM and the 4GB of RAM unity can use on my win7 64bit os left enough space

neither do I… my project is not so big but i have something like 600 textures of 50k each (~30MB in total) and every texture when loaded in unity takes about 5MB… so my ~30MB suddenly became ~3GB only for textures…