Unity内存泄漏成因及修复咨询:运行时内存超64GB致崩溃
Hey, let's get to the bottom of why your Unity instance is eating up 64GB of RAM and crashing—this is a classic resource cleanup issue, and it's easy to fix once you know where to look.
What's Causing the Leak?
First, let's break down the two big problems in your code:
- Every frame, you're creating a new
Texture2Dand never getting rid of it: Unity's texture objects aren't just plain C# objects—they hold onto GPU and CPU memory that the regular garbage collector can't automatically free. So every time you runnew Texture2D(...)inUpdate(), you're leaving a chunk of memory hanging around forever. - Your
WebCamTextureisn't being cleaned up: Even if you fix the frame texture, the webcam resource might linger if you don't explicitly stop and destroy it when your object is removed from the scene.
Step-by-Step Fixes
1. Destroy the Frame Texture Immediately (Minimum Fix)
If you want to keep creating a new texture each frame (though I don't recommend it for performance), you must call Destroy() on it after you're done using it. Here's the fixed Update():
void Update () { if (wct.didUpdateThisFrame == false) return; ++frameNumber_; Texture2D frame = new Texture2D(wct.width, wct.height); frame.SetPixels(wct.GetPixels()); frame.Apply(); // Do your processing with 'frame' here—display it, analyze pixels, etc. // Critical line: Free the texture's memory Destroy(frame); }
2. Reuse the Texture (Better for Performance & Memory)
Creating a new texture every frame is inefficient. Instead, make one texture in Start() and reuse it each frame. This eliminates constant memory allocations entirely:
private WebCamTexture wct; private int frameNumber_; private Texture2D frameTexture; // Reusable texture void Start () { frameNumber_ = 0; // Initialize webcam texture as before wct = new WebCamTexture(WebCamTexture.devices[0].name, 1280, 720, 30); Renderer renderer = GetComponent<Renderer>(); renderer.material.mainTexture = wct; wct.Play(); // Create the frame texture ONCE, matching webcam dimensions frameTexture = new Texture2D(wct.width, wct.height); } void Update () { if (wct.didUpdateThisFrame == false) return; ++frameNumber_; // Reuse the existing texture instead of making a new one frameTexture.SetPixels(wct.GetPixels()); frameTexture.Apply(); // Use frameTexture for your needs here }
3. Clean Up WebCamTexture When Done
Add an OnDestroy() method to make sure the webcam resource is properly released when your object is destroyed (like when you exit the scene):
void OnDestroy() { // Stop and destroy the webcam texture if it exists if (wct != null) { wct.Stop(); Destroy(wct); } // Clean up the reusable frame texture too if (frameTexture != null) { Destroy(frameTexture); } }
Why This Works
Destroy()is non-negotiable for Unity's engine-managed objects like textures—it tells Unity to release the underlying GPU/CPU memory that the C# GC can't touch.- Reusing textures avoids the overhead of constant allocations and deallocations, which also helps with frame rate (fewer garbage collection spikes).
- Cleaning up
WebCamTextureensures the camera hardware and its associated resources are freed, preventing any lingering memory leaks from the device itself.
内容的提问来源于stack exchange,提问作者aCuria




