- Home /
Memory Leak with creating a new Textrure2D
Hi all, simply making a new project and attaching the following code to a gameobject:
float time = 0;
void Update()
{
time += Time.deltaTime;
if(time >= .5f)
{
time = 0;
Texture2D screenShot = new Texture2D((int)1000, (int)1000, TextureFormat.RGBA32, false);
}
}
Anyone know why this makes the memory continuously increase? As far as I can tell, you cannot deallocate a Texture2D's memory -- it handles itself if it is not referenced by anything. The previous "screenshot" is not referenced by anything after the next iteration of the Update loop.
Answer by JakeCaron · Dec 18, 2017 at 06:45 PM
So I looked more into this and Texture2Ds are not automatically handled if they do not have a reference! However, you can reallocate the memory by using, "Texture2D.Destroy();". Hope this saves someone time in the future.
Answer by Shrikky23 · Dec 18, 2017 at 06:53 PM
Textures are Resources, please Resources.UnloadUnusedAssets() or Unload that particular Resource manaully using Resources.UnloadAsset().
Your answer
![](https://koobas.hobune.stream/wayback/20220612144818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Memory consumption after Texture2D.LoadImage and Destroy texture 0 Answers
Kinect SensorData Memory Leak 1 Answer
cleanup allocated memory for texture2D 1 Answer
How to release memory of Texture2D dynamic object, or is there a memory-leak? 0 Answers
Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers