How come every object in Unity I load, takes roughly 250% of it’s size in memory, than it takes on the disc?
For example, loading two textures totaling 6MB increases memory usage for about 16MB. I only used Windows Task Manager to check memory usage.
This happens in pretty much every loading “situation”: clicking on an object in Unity Editor, loading using Resource.Load or loading from AssetBundles.
So back to my original question, why does this happen?
were those two textures in BMP format or did you enable texture compression in unity?
Because textures that are beeing loaded are either DXT compressed or are bmp, there is no intermediate format. PNG / JPG will only be compressed on the disk.
Guess this is the case as 3MB is no valid pow2 texture format unless its 1024x512 uncompressed
They are one 2k texture and one 1k texture(diffuse + normal map), thus 6MB.
They were in tiff format before importing into unity, and in unity they have been compressed DXT5(diffuse) and DXT1(normal).
Original texture sizes are 16MB and 1.36MB(tiff), compressed sizes are 5.3MB and 700KB(in Unity).
EDIT: Your response got me thinking, and it does seem that unity will load non-DXT version of the texture into system memory…meaning 17.36MB get loaded into system memory, while the 6MB version will be used for video memory only.
I always assumed the memory used would be the same as it was shown in the Texture Importer and completely ignored the original size of the textures.
But it is counter-intuitive if you ask me. Why would I need the original texture in system memory if the compressed DXT version is being shown in-game?
But at least I figured it out, guess I gotta compress all my textures into DXT manually before importing.
I did an experiment that seems to confirm my theory.
Two exactly the same textures:
One saved as TIFF(RGB), 12MB size. Compressed in Unity it is 2.7MB DXT1 texture.
Other saved as JPG(RGB), 1.58MB size. Compressed in Unity it is 2.7 DXT1 texture.
Loading texture #1 increased my memory usage by ~12MB.
Loading texture #2 increased my memory usage by ~2.5MB.
I only tried this by clicking the textures in the editor(once you click them, Unity loads them and memory usage increases). I don’t have time to test it using Resources.Load or AssetBundles right now, but I never noticed memory use difference between using either of these three methods.
But I urge someone to try. I’ll try it myself tomorrow.
Well in the editor you work with the original assets and the whole handling works differently then in the built player as it has to fullfill completely different needs than the player (you don’t want to wait all the time until some asset appears).
In the editor you will commonly use significantly more memory than in the game as you work with significantly more data than you will have active in the game
You’re right, this only happens in editor(referring to my “experiment”).
However, the original problem remains, whether using editor or standalone version of the game.
I had a issue with asset bundle unloading so that accounted for the part of the extra memory use, but I still get pretty much exactly 200% memory use than the actual texture size.
Simple script
public var MR : MeshRenderer;
function OnGUI()
{
if(GUILayout.Button("Load textures"))
Resources.Load("FH_BlacksmithWorkshop_Diffuse_2k");
}
will increase the memory by ~11MB, while the FH_BlacksmithWorkshop_Diffuse_2k is 5.3MB.