- Home /
How do you free a manually created texture?
This is probably trivial, but I'm loading a very very large texture like this:
// Load huge tga map as an image.
var i = new nImage();
i.Load("map.tga");
// Create texture from subsection of image
Texture t = i.Texture(640, 640, 1200, 1200);
Afterwards I want to dispose of the texture I've created. Normally if I loaded a texture using Resources.Load() I'd use Resources.UnloadAsset() to dispose of it.
However, this texture is created using (internally to nImage):
public Texture2D Texture (int x, int y, int width, int height)
{
var bytes = Section (x, y, width, height);
if (bytes == null)
return null;
var texture = new Texture2D(width, height, TextureFormat.ARGB32, true, true);
texture.SetPixels32(bytes, 0);
texture.Apply();
return texture;
}
How do I force the texture to be removed from the graphics memory of the card?
(It doesn't appear to be automatic, because I'm hitting memory exhaustion errors on the graphics card after a while).
am force remove texture well restarting system does that as all memory(RA$$anonymous$$) is erased
while the HDD stuff remains
Answer by ryba · Jan 24, 2013 at 06:06 AM
You dont free anything manually in C#. Memory is managed by mechanism called Garbage Collector, it releases memory at its own time, you can however make a request for garbage collector to start collecting unused memory by calling GC.Collect(); But be aware it doesnt clean up memory, only informs garbage collector that you want him to collect mem. You can also use GC.WaitForPendingFinalizers() method, that will perform similiar to GC.Collect(), but also it will freeze your application untill cleanup is performed, so its most like direct cleanup method, but not exactly, also keep on mind that calling WaitForPendingFinalizers(), especially while game is running, is rather bad idea, since it can freeze your app badly.
You can read more about it here http://docs.unity3d.com/Documentation/Manual/UnderstandingAutomaticMemoryManagement.html
About Resources.UnloadAssets() - i dont know what does it exatly do but i assume it closes opened IO resources, like file descriptors, rather then really free memory (however its releated).
The issue is that the graphics memory seems to be used by creating Texture2D objects, but never released.
Answer by madc9ke · Oct 23, 2017 at 12:20 PM
I just stumbled upon this problem when working on my dual quaternion skinning script.
I use textures to pass data to GPU for calculations and retrieve results.
After optimizing everything else the biggest bottleneck is GC.Collect() when using GetPixels (code below)
Answer by sonnyb · Dec 23, 2021 at 04:32 PM
I think you need to call Destroy
on the manually created texture.