【发布时间】:2018-03-16 08:18:28
【问题描述】:
我在使用 Unity 时出现内存泄漏,代码如下:
行为:在运行时,Unity 最终使用 > 64GB 的 RAM 并崩溃(这就是这台机器的全部)
场景:我的场景中只有一架飞机,代码如下。
我该如何解决这个问题?我不认为我想要一个弱引用,(或者至少在 C++ 中我不会在这种情况下使用 weak_ptr,也许是 unique_ptr)。我只想在对象超出范围时释放内存。
void Start () {
frameNumber_ = 0;
// commented code leaks memory even if update() is empty
//WebCamDevice[] devices = WebCamTexture.devices;
//deviceName = devices[0].name;
//wct = new WebCamTexture(deviceName, 1280, 720, 30);
//GetComponent<Renderer>().material.mainTexture = wct;
//wct.Play();
wct = new WebCamTexture(WebCamTexture.devices[0].name, 1280, 720, 30);
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = wct;
wct.Play();
}
// Update is called once per frame
// This code in update() also leaks memory
void Update () {
if (wct.didUpdateThisFrame == false)
return;
++frameNumber_;
Texture2D frame = new Texture2D(wct.width, wct.height);
frame.SetPixels(wct.GetPixels());
frame.Apply();
return;
【问题讨论】:
-
我不擅长团结,但我已经看到了一些可疑的东西。您正在创建新的
Texture2D每一帧。那肯定会堆积垃圾