- Home /
What is the correct way to remove assets from memory?
I'm having some trouble finding the correct way to handle this, and thought I'd see if anyone on here has some insight.
Basically, I'm loading Facebook profile pictures from the internet using the WWW class, like so:
// Start downloading the image and wait.
WWW www = new WWW(imageUrl);
yield return www;
// Save texture;
if(www.textureNonReadable != null)
{
picture = www.textureNonReadable;
}
I've found that when I load pictures in this way, they do not seem to get garbage collected automatically. I imagine I need to "delete" the texture asset for it to be removed from memory. Problem is, there are a number of ways to do this:
GameObject.Destroy(picture);
GameObject.DestroyImmediate(picture);
Resources.UnloadAsset(picture);
picture = null;
Even trying each of those options, I've found using the profiler that the texture seems to only truly be removed from memory when I call "Resources.UnloadUnusedAssets( )".
So, ultimately, for reference, what is the correct way to remove assets like this from memory and avoid a memory leak? Or should the picture be garbage collected automatically when all references are "null" as expected?
Answer by Eric5h5 · Oct 16, 2013 at 08:29 PM
You answered your own question...Resources.UnloadUnusedAssets. Unity objects are not part of Mono and are never garbage collected on their own, although loading a new level will destroy all objects and do Resources.UnloadUnusedAssets.
Ok, gotcha. Is Resources.UnloadAsset() supposed to work like "UnloadUnusedAssets" for a single asset? Because in testing, it didn't seem to behave that way (after calling UnloadAsset, the asset seemed to remain in memory, according to the Unity profiler).
I've never used it, but according to the docs, it's only for assets that are stored on disk.
Your answer
Follow this Question
Related Questions
the texture appear rubbish 1 Answer
Free memory after WWW.LoadImageIntoTexture 1 Answer
Large memory footprint increase when assigning a GUITexture from a WWW Object on iPad 1 Answer
Loading imagesequence at runtime - memory problem 1 Answer
Why do larger www textures take huge amounts of memory? 1 Answer