Unity Profile 内存不断增加问题

Posted B612灯夫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity Profile 内存不断增加问题相关的知识,希望对你有一定的参考价值。

原始问题是这样的

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

以上是关于Unity Profile 内存不断增加问题的主要内容,如果未能解决你的问题,请参考以下文章

2星|《戏说统计》:哲学老师的统计学课讲稿,复杂,罗嗦,不断跑题

为啥我的 Unity ios 应用程序的文档和数据不断增加?

为啥 iOS AudioQueue 内存不断增加?

小松教你手游开发unity实用技能Unity Mesh更新的时候增加内存

tensorflow内存消耗不断增加

更改页面时,SwiftUI TabView 内存占用不断增加