- Home /
Runtime Generated Textures Not Releasing Memory
Hello
My textures that I download at runtime are not releasing memory when I destroy them. Can you tell me what is going wrong and how I can release the texture (.png) memory?
I have an example scene and 1 script that very nicely & simply demonstrates the problem. When the user presses the space bar, we download a texture (satellite image) and apply it to a cube (we delete the cubes existing texture to release that memory).
I am profiling my .exe using the task manager and it shows that whenever I press space bar that the memory always increases (by about 1mb) and doesn't decrease or stay constant. So the texture memory isn't being released.
Memory Profile Results:
- Game Open: 27mb
- First Space bar press: 30mb
- Second Space bar press: 31mb
Any advice on how I can release the textures memory or achieve any memory savings?
My script and scene can be downloaded here:
Simple Script:
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
StartCoroutine( downloadFile("http://maps.google.com/maps/api/staticmap?center=-33.7600944982221,151.219708834037&zoom=18&size=512x512&scale=1&maptype=satellite&format=png&sensor=true&") );
guiTxt.text = "Downloading Texture";
}
}
IEnumerator downloadFile(string url) {
WWW www = new WWW(url);
while(!www.isDone)
yield return www;
if (www.isDone) {
DestroyImmediate(GameObject.Find ("Cube").renderer.material.mainTexture, true);
System.GC.Collect();
Resources.UnloadUnusedAssets();
GameObject.Find ("Cube").renderer.material.mainTexture = compressTexture(www); //www.texture;
guiTxt.text = "New Texture Applied";
}
}
Texture compressTexture(WWW www) {
// Post: My method to compress textures that are currently used/displayed in game
// Without this code the texture uses 1.1 megabytes!!! With it, it uses about 300k
Texture2D texture = new Texture2D(2, 2, TextureFormat.RGB24, false); // size params can be anything AFAIK
www.LoadImageIntoTexture(texture);
texture.Apply(false, false);
texture.Compress(true);
return texture;
}
When sharing project resources you should export a package ins$$anonymous$$d of just a scene. The scene doesn't actually contain the script or it's settings, but an exported package will include all of the scene's dependancies as well as their scene-specific idiosyncrasies.
The WWW class has a textureNonReadable flag which saves memory as well, because Unity doesn't need to store two versions of the texture in memory (one to edit, and the original). Based from what's available in the documentation I'm not sure that the LoadImageIntoTexture method does this or not, so you may want to do some more sleuthing on your own. Just a heads-up on that vs the usual www.texture field.
Finally, the following is a quick and dirty reworking of your code that demonstrates the memory is reclaimed without having to call Resources.Unload or do a Garbage Collection.
using UnityEngine;
using System.Collections;
using System.Configuration;
public class WWWTextureTest : $$anonymous$$onoBehaviour
{
public GameObject cube;
const string I$$anonymous$$AGE_URL = "http://maps.google.com/maps/api/staticmap?center=-33.7600944982221,151.219708834037&zoom=18&size=512x512&scale=1&maptype=satellite&format=png&sensor=true&";
private bool forceFullCollection;
void Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift))
StartCoroutine(DownloadImage(I$$anonymous$$AGE_URL, forceFullCollection));
else
StartCoroutine(DownloadImage(I$$anonymous$$AGE_URL));
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
forceFullCollection = !forceFullCollection;
}
IEnumerator DownloadImage(string url, bool forceFullCollection)
{
yield return StartCoroutine(DownloadImage(url));
Debug.Log(string.Format("Total $$anonymous$$emory Used ({0} collection): {1}", forceFullCollection ? "full" : "partial", System.GC.GetTotal$$anonymous$$emory(forceFullCollection)));
}
IEnumerator DownloadImage(string url)
{
WWW www = new WWW(url);
while(!www.isDone)
yield return www;
cube.renderer.material.mainTexture = www.textureNonReadable;
}
}
Your answer
Follow this Question
Related Questions
Download images from url, save to device, load into UGUI 4 Answers
New blank project with 3 leaked textures ??? 1 Answer
Assigning UV Map to model at runtime 0 Answers
Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers
Corrupted/disarrange 2D Texture after app is built 0 Answers