- Home /
Possible Texture memory leak
I'm having a major memory leak issue possibly related to texture memory
In our program, a large number of textures are loaded into a list at the beginning on the program. The user can select one of these images and it pops out onto a moveable tile that can then be closed. Unfortunately the new tile seems to use more memory and doesn't release it afterwards.
The function that sets the texture of the new tile is effectively this:
public void initTexture(texture _input)
{
renderer.material.mainTexture = _input
}
In my main script I call both Resources.UnloadUnusedAssets() and GC.collect
Is there anything I've missed? Is there some specific way to unload the texture? I was under the impression Unity only held one copy of each texture, does my tile class create and store a new version of it?
Answer by Kolodej · Jan 10, 2014 at 12:20 PM
Hi, consider using Texture2D.Destroy / DestroyImediate. It should be working properly.
private void UpdateMaterialMainTexture(Texture2D texture) {
// destroy old if exists
if (_currentMainTexture != null) {
Texture2D.DestroyImmediate(_currentMainTexture, true);
}
// assign a new texture
_currentMainTexture = texture;
// set a new material texture
this.renderer.material.SetTexture("_MainTex", _currentMainTexture);
}
I don't actually store the image in the script itself, the images are loaded into a List in the content script, and passed into my function when the "Tile" is created, where it manually sets the Tile's texture to said image.
As such, there is nothing to destroy, and attempting to destroy the tiles renderer.material.mainTexture gives a warning about deleting assets.
At present we are getting around the problem by restarting the program at midnight every night and avoiding spawning excessive numbers of assets, but this is far from ideal, as large videos are also loaded into memory at some points!
EDIT: I can also confirm that they are the same texture, as calling "Resources.UnloadAsset(Tile.renderer.material.mainTexture)" deletes the asset on all individual copies of the tile.
I've been researching this and have found the issue on a number of questions, I think it is more likely to be an issue with Instantiating and Destroying prefab objects
Your answer
Follow this Question
Related Questions
Runtime Generated Textures Not Releasing Memory 0 Answers
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