- Home /
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
What happens when you do this without the unload unused assets? If you look at the texture display it says that it added exactly 4$$anonymous$$B texture memory.
@spree 1 , If I do this without the unload unused assets, it still added more than 8$$anonymous$$B total memory. But in fact, I just create a 1024*1024 Texture.
Curious. Have you tried building the app, then running it standalone outside the editor and monitor the memory increase in the Task $$anonymous$$anager (on Windows, at least) at the point where you know the texture gets created?
Answer by Waz · Aug 04, 2011 at 12:10 PM
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.
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 8$$anonymous$$B+ 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 (update$$anonymous$$ipmaps : 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 $$anonymous$$emory) 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 $$anonymous$$d 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. VRA$$anonymous$$ is virtualized anyway and not something that Unity needs to involve itself with in that way.
Answer by Statement · Aug 04, 2011 at 09:56 AM
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.
If I create two textures, It added about 20$$anonymous$$B total memory
Wait, what, so 1 texture (estimated 4$$anonymous$$B) added 8.8$$anonymous$$B, and 2 textures (estimated 8$$anonymous$$B) added ~20$$anonymous$$B (17.6$$anonymous$$B)?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Memory issues 1 Answer
Does Animator with Sprite Animation clip cause memory issue ? 0 Answers
Using baked textures and about texture sizes? 1 Answer
Texture Doubled in size in RAM Profiler 3 Answers
maximum slice in a sprite sheet 1 Answer