- Home /
Default-Diffuse material leak - how to find source?
Hello,
I have this highly convoluted project. Now recently I noticed, that when running it, it slowly gobbles up memory until finally either Unity or the game crashes from lack of memory.
So I used the DetectLeaks script and found out that the game creates like.. a few thousand materials per second. All called "Default-Diffuse (Instance)". I can "find" and destroy them, nothing in the game seems to change, but the memory problem is fixed, except for the performance hit of finding and destroying them in the first place.
But of course I'd rather be interested in finding out, where they come from. So, does anyone have an idea how I could find the object/script responsible for creating them?
And no, turning more and more parts of the game off will not work, as it is very entangled and one thing doesn't work without the other (hooray..). Please share your brilliant insights!
Okay, I have a first lead. I'm changing the color of some materials from an animation. If I don't play that animation, nothing leaks.
Answer by Orion 1 · Jun 28, 2010 at 03:18 PM
Well. Through try and error I found the cause of the problem to be an animation, that fades the color of the material of an object. It seemed like in some cases the material could actually not be found from the animation (modified the subobjects).
Instead of using an animation, I now fade the material with an Enumerator, which works good enough.
Lesson learned: if I could start this project over, I'd use a lot less animations for mundane tasks.
Answer by Mike 3 · Jun 28, 2010 at 10:38 AM
First thing I would do is a project wide search for new Material(
After that it gets a little trickier, but you'll probably want to look for Instantiate, and see if any of them have materials as the object to instantiate
Not the most ideal methods I know, but I can't think of any easier ways
Okay, well I'm not explicitly creating new $$anonymous$$aterials, so there goes the first one.
I have loads of Instantiate calls, but I only ever instantiate GameObjects and prefabs (also GameObjects).
I did hear that Unity sometimes creates materials just for giggles when you call renderer.material Which happens an awful lot. So often in fact, I have no idea how I could find out, which one could cause problems (or how to see THAT it causes problems).
it only creates the new material the first time you call .material, and it cleans up after itself when you destroy the object
Hm. That's off the list then (I suppose). Then there's something I heard that if I change properties on a material that it creates a new instance. Can that be right?
$$anonymous$$g. I do renderer.material.color = Color.Blue; And it turns it into an instance? I have a script that fades the color from black to white. So I'd have a new instance every frame?
It does it once (It creates a new instance if material == shared$$anonymous$$aterial). I think your issue is elsewhere :(
Do you know what this is all about? http://answers.unity3d.com/questions/10922/do-i-need-to-manually-destroy-material-model-instances-that-are-lazily-copied