Tier 1 Series 5

5E: Memory Leaks

Pilot Record
Student Profile
"A hangar that only accepts planes but never lets them leave will eventually burst. If your collections grow every frame but are never emptied, you've created Memory Ghosting. A skilled Engineer audits for Collection Sanitation."

The Concept: Life-Cycle Management

Every collection has a life-cycle. In Unity, scripts often accumulate data (like logs, trail points, or nearby targets) that is only relevant for a short time. If you don't explicitly remove this data, it remains in memory forever, leading to a "Leak" that slowly kills performance.

Clear on Reset: Whenever a mission restarts or a drone is disabled, use .Clear() on all Lists and Dictionaries.
Audit the Add: Every time you see .Add() in an AI-generated script, search for the corresponding code that removes or clears that item.
Nullify References: If a collection holds references to game objects that were destroyed, clear them immediately to prevent "Null Reference" ghosts in your memory.
Red Flag Detected

The AI Trap: "The Permanent Passenger"

You ask the AI: "Record the positions of the drone so we can draw a flight path."

// AI-Generated Code: A slow-motion memory leak
List<Vector3> flightPath = new List<Vector3>();

void Update() {
    // Audit Fail: This list grows by 60 items per second.
    // After 10 minutes, there are 36,000 points in memory.
    flightPath.Add(transform.position);
}

void OnDisable() {
    // Audit Fail: The list is never cleared. 
    // If the drone respawns, the old data is still there!
}

This is "Resource Hoarding". Even with 20GB of storage, your computer's active RAM is limited. These leaks cause "Stuttering" as the system struggles to manage the massive, useless data piles.

Elite Telemetry

Research shows "Elite" teams achieve 15% faster lead times by keeping AI on a "very tight leash."

  • Small Batches Solving one problem at a time prevents logic drift.
  • Modular Design Localizing the "blast radius" of AI changes.
  • Tight Loops Rapid iteration with constant code review.

The Mechanic's Correction

Corrective Protocol
void OnDisable() {
    // Corrected: Sanitation pass. 
    // Releases memory back to the system.
    flightPath.Clear();
}
Your Pilot Command
> Use the Unity Profiler to identify and eliminate the specific bottleneck.
Next Mission
The Inspector List