Why Does the Number of Material Instances Keep Increasing During Runtime?
Why does the number of Material Instances keep increasing after the project has been running for a while, and how should this be investigated? https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/b779b532 269e 403c b042 931e480eb81b.png
Start by checking whether new instances are continuously being created during runtime. In the report, a certain type of skill material increases from more than 100 at the beginning to more than 2,000 over time, with new instances continuing to appear. This should be investigated together with the lifecycle of skills, characters, and VFX objects to determine whether new objects are continuously created during runtime. In Unity, accessing can instantiate a new independent when the is currently using a shared material. This means that not only modification operations such as and require attention; simply accessing can also trigger material instantiation. If the material only needs to be read or the shared material should continue to be used, check whether should be used instead. Another easily overlooked issue is that the lifecycle of a material is not completely tied to the lifecycle of its . A instantiated at runtime through APIs such as does not automatically complete resource cleanup when the corresponding is destroyed. These dynamically created materials therefore need to be managed separately. For example, a skill may create a temporary VFX object and instantiate a material at the same time. After the skill ends, the is destroyed, but the is not explicitly destroyed. The next skill activation then creates another instance. After the project runs for a long time, the number of objects can increase from hundreds to thousands. Unused materials may also be reclaimed during resource cleanup such as , but this should not be treated as the primary lifecycle management mechanism for frequently created dynamic materials. https://uwa overseas public.oss us east 1.aliyuncs.com/uploads/markdown/74535e7e dbf1 4de1 8c50 723b4420366c.png To investigate the issue, start with the material names that show continuous growth and correlate them with operations such as skill activation, character spawning, and VFX playback. If performing an operation repeatedly increases the number of a particular , and the instance remains after the corresponding object is destroyed, continue tracing the material's creation and destruction lifecycle. Optimization can focus on two areas: Reduce unnecessary creation. Check whether is being accessed only to read material properties. If different renderers only need different per renderer properties such as color or values, evaluate whether is appropriate to reduce independent material instances. If the material needs to switch its or , cannot simply be used as a replacement. Explicitly manage the lifecycle of dynamic materials. If a must be instantiated, retain a reference to it and explicitly call when its owning object's lifecycle ends. For frequently created skills, characters, and VFX objects, also check whether materials can be reused instead of continuously creating new instances.