Unity Profile 内存不断增加问题

本文讨论了Unity编辑器中内存泄漏问题,特别是TotalObjectCount的含义及其增加的原因。通过实例展示了如何识别和解决由Material引用导致的内存泄漏问题,并提供了避免内存泄露的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原始问题是这样的

unity profiler total object count regarding renderer material memory leak

What exactly is the Total Object Count in unity's profiler keeping track of? If I make a simple scene (no main camera) that has 1 game object with a MonoBehaviour script attached (running no code), and I run the scene, then select my game object in the scene hierarchy, the Total Object Count increases by 10. Then, when I deselect my game object in the scene hierarchy, the Total Object Count decreases by 7 (a net increase of 3). If I keep doing this over and over again, my Total Object Count will just keep increasing indefinitely as far as I can tell. The documentation for the profiler states:

"Object Count is the total number of Objects that are created. If this number rises over time then it means your game is creating some objects that are never destroyed."

The entire reason I made this small project was to try and track down some memory leak issues in my current unity project. Maybe if I understood what the Total Object Count field was being used for and what the expected behavior is, I could use it more effectively as a debugging tool.

比较靠谱的回答

Ok, so I found the cause of our problem and a sort-of work around. It basically stems from accessing a material on a renderer by making a call: Renderer.material or Renderer.materials[0]. When you access the material on a renderer in either of these ways, the object count in the unity editor seems to increase. If you call DestroyImmediate on the gameObject this renderer is attached to, the object count does NOT go down. Instead, you MUST call DestroyImmediate on Renderer.material and THEN call DestroyImmediate on the gameObject to clear up all object counts.

The following code snippit reproduces the problem (and offers a variety of solutions / possible bugs). This script assumes you attach it to a gameObject in your scene and supple the public variable "publicObject" with a gameObject with a Renderer Component with a Material. Let me know if there are any questions or if something is unclear!

以及测试代码

using UnityEngine; using System.Collections;

public class DestroyTest : MonoBehaviour {

GameObject objectCopy1 = null; GameObject objectCopy2 = null; GameObject objectCopy3 = null;
 

public GameObject publicObject = null;
 

// Use this for initialization void Start() { objectCopy1 = (GameObject) Instantiate(publicObject); objectCopy1.renderer.material.mainTextureScale = Vector2.zero; objectCopy1.active = false;
 

     objectCopy2 = (GameObject)Instantiate(objectCopy1);
     objectCopy2.active = false;
     objectCopy3 = (GameObject)Instantiate(objectCopy2);
     objectCopy3.SetActiveRecursively(true);

 

}
 

void Update() { Renderer renderer = objectCopy3.GetComponent<Renderer>();
 

     //WORKS
         //Material material = renderer.material;
         //DestroyImmediate(renderer.materials[0]);
     //LEAK
         //Material material = renderer.materials[0];
         //for (int i = 0; i < renderer.materials.Length; ++i)
         //{
         //    DestroyImmediate(renderer.materials[i]);
         //}
     //LEAK
         //Material material = renderer.material;
         //for (int i = 0; i < renderer.materials.Length; ++i)
         //{
         //    DestroyImmediate(renderer.materials[i]);
         //}
     //WORKS
         //Material material = renderer.materials[0];
         //DestroyImmediate(renderer.materials[0]);
     //WORKS
         //Material material = renderer.materials[0];
         //DestroyImmediate(renderer.material);
     DestroyImmediate(objectCopy3);
     objectCopy3 = (GameObject)Instantiate(objectCopy2);

 

} 

}
总结一下:就是在unity中,程序写的都没有问题但是程序跑起来,内存却不断的被消耗,直至程序崩溃,打开Unity的profile可以看到下面的


这里面 Total Object Count 不断增加,当然内存也是不断增加的

而下面给出的答案,原因在于 你在程序中调用了Renderer.material or Renderer.materials[0].就是说,你重复的调用GameObject的Renderer组件,但是即使你调用了Destory()也是没有用的,在内存中这块内存并没有被注销掉

当然,上面的大神也给了解决方案,自己看代码,我也就不罗嗦了

但是我不想每次都Destory掉Material,在我的代码中用到了,Resource.Load()相对应的,只要调用

Resources.UnloadUnusedAssets();

然后就没问题了

thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值