- Home /
How to unload materials, textures and meshes from memory?
Hey there, I'm baffled at how hard it seems to handle memory correctly in Unity.
The type of game I'm working on had me separate different assets in different prefabs, sounds, sprites, animations, etc. which I instantiate and destroy once I'm done using them. I'm fine with the performance of all the instantiation and I call GC.Collect every now and then.
The problem I'm facing is that the memory used goes up and never goes down. I've been tracking the problem and the number of textures, materials and meshes in memory just keep growing, never being released from memory. I end up in a situation where, gradually, it's like I had loaded every resources in the first place. And of course it crashes before that.
Anyone has experience with unloading materials, textures and meshes out of memory once they're not used anymore?
Whoops! $$anonymous$$anaged languages. You have to wait for the monster of the deep to decide that you're no longer using the objects in question... I think there's a way to enable manual memory management, but I'm not sure if Unity allows you to do that...
Yeah the problem is that the monster of the deep does his GC correctly every now and then....but from what I read, materials, textures, meshes are not GCed because unity still keep references to them. So say I make my character eat an apple, so I load a few prefabs for apple sounds, apple graphics, apple animations, etc. The texture and material with the apple stuff is loaded...the apple animation ends, I destroy all these prefabs...good? no...the apple material and texture are sitll in memory. I make my character play with a doll, ah...material and texture for doll loaded...never unloaded, etc until the app crashes out of memory. I've read about Resources.UnloadUnusedAssets but that didn't help and was slow as hell. I'm at a loss and would really love some support to get those assets out of memory when I don't need them.
I thought UnloadUnusedAssets could be used as an AsyncOperation, meaning you can just fire it off and get on with the rest of your life? Unless, as you say, it just doesn't work the way you need it to...
Well it sure would be nice to make it async if it did free up the assets when I use it but it doesn't. I'm sure it does in the right circumstances. But I have no idea what those circumstances are. All I know is that this is way more complicated than it should be. Unused assets, please leave my memory alone :D
Ooh, Resources.UnloadUnusedAssets only works on the Resources folder! $$anonymous$$aybe you could use that? Except that the resources folder is usually exempt from the normal memory management...
Your answer