Why does a 1024*1024 Texture2D use so much memory?

Why does a 1024*1024 ARGB32 Texture2D use more than 8MB total memory? I think the correct cost is 4MB (4MB = 1024 * 1024 * 32bit / 8 / 1024 / 1024).

I just did a test on Unity3.3 pro. Create an empty project only included a empty scene. Create bellow script:

using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {

	public Texture2D tt;
	void OnGUI () {
		if (GUILayout.Button("Create Texture2D")) {
			tt = new Texture2D(1024, 1024, TextureFormat.ARGB32, false);
			Resources.UnloadUnusedAssets();
		}
	}
}

Drag this script to scene.

When I click the button on run time. The profiler of unity display that Textures memory add 4MB but Total memory add more than 8MB.

Here are two screenshots:

Before Create a 1024*1024 ARGB32 Texture2D:

Total: 270.5 MB Delta: 0 B
Texture: 770 / 2.5 MB
Meshes: 26 / 74.9 KB
AnimationClips: 0 / 0 B
Total Object Count: 1134

After Create a 1024*1024 ARGB32 Texture2D:

Total: 279.3 MB Delta: 0 B
Texture: 771 / 6.5 MB
Meshes: 26 / 74.9 KB
AnimationClips: 0 / 0 B
Total Object Count: 1116

Have you got mipmaps on?

–

What happens when you do this without the unload unused assets? If you look at the texture display it says that it added exactly 4MB texture memory.

–

Curious. Have you tried building the app, then running it standalone outside the editor and monitor the memory increase in the Task Manager (on Windows, at least) at the point where you know the texture gets created?

–

2 Answers

2

Texture2D’s created in code are read-write, so there is a copy outside texture memory, just as for an imported Texture2D with isReadable set to true. When you have finished modifying the Texture2D, use the makeNoLongerReadable parameter of Texture2D.Apply() to discard the in-memory copy.

Ah, this makes some sense.

–

I tried the Apply(false, true); right after creating the texture. (and even try to set a pixel before it to make sure it indeed need "apply"), but profiler still shows overview consumed 8MB+ memory. I did call Resources.UnloadUnusedAssets(); and GC.Collect() after apply, no help. I can only assume that is a "bug" of texture 2D implementation. Refs, function Apply (updateMipmaps : boolean = true, makeNoLongerReadable : boolean = false) : void

–

Unity seems to keep a copy in the general ram in order to quickly be able to re-upload it into the video ram. Notice that the video ram (Texture Memory) is very rapid in following changes in the scene and I suppose this is the Unity philosophy of dealing with this kind of problem. I assume this could even be working together with culling. Personally I don't need this for everything, for example GUI textures that are always visible. And I don't mind having to reload popup textures. Really it's mostly just annoying.

–

It doesn't keep a copy if you use the makeNoLongerReadable parameter like Warwick Allison said. VRAM is virtualized anyway and not something that Unity needs to involve itself with in that way.

–

I am not sure if temporary garbage adds to the count or not. If it does, then those 8+ MB might actually be to some part garbage that haven’t been collected.

You could try to collect all garbage to see if it does any difference.

using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
    public Texture2D tt;
    void OnGUI () {
        if (GUILayout.Button("Create Texture2D")) {
            tt = new Texture2D(1024, 1024, TextureFormat.ARGB32, false);
            Resources.UnloadUnusedAssets();

            // ************************************************************
            // Lines below are added to collect garbage and wait for thread
            // ************************************************************

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

I find it a bit "scary" to see a doubling in memory usage from the expected results. What happens if you create 2 textures? It could maybe be some pooling algorithm that allocate memory chunks up in advance.

–

Wait, what, so 1 texture (estimated 4MB) added 8.8MB, and 2 textures (estimated 8MB) added ~20MB (17.6MB)?

–

I just tested this on my machine and got the same result in the profiler. Deeply disconcerting, actually. Looking forward to some wizard's explanation.

–