Low Level API to clean memory

Hi!

I have a little question regarding an “out of memory problem”. I need to create dynamically a large number of gameobject (mesh renderer + mesh collider + texture) and I cannot destroy them using the Destroy command on the entire GO to clean the device memory.

There is a trick to delete ONLY texture from memory?

I’m not really clear on what you’re trying to do by only removing the texture, this will just make objects appear blank in most cases, but you can set renderer.material.mainTexture to null, and then if nothing else is using that texture, call Resources.UnloadUnusedAssets(). That should force unload the texture from memory.

IIRC, Unity resources such as textures are not garbage collected; they’re instead reclaimed on scene changes, otherwise you need to reclaim them via Destroy calls on the created resources themselves (not the game object).

Destroy doesn’t do anything for reclaiming memory; you need Resources.UnloadUnusedAssets for that.

–Eric

Yes I know…in fact I move these on an invisible layermask before remove the texture!

However

  1. maintexture = null
  2. resources.UnloadUnusedAssets()

What about garbage collector? UnloadUnusedAssets is sufficient?

That will only set a variable to null, it won’t get rid of the texture. Unity objects need to be destroyed, Mono objects (variables) are garbage collected when they go out of scope. Unity objects are not garbage collected even when destroyed, since they aren’t part of Mono (that’s where UnloadUnusedAssets comes in). You don’t need to, and apparently shouldn’t, set variables to null in .NET (unless there’s a specific reason of course)…it doesn’t help and might actually hinder garbage collection. Mono may or may not have the same behavior. Anyway, setting things to null doesn’t destroy Unity objects.

–Eric