Tier 4 Series 301

301F: The Garbage Collector

Pilot Record
Student Profile
"In a long-range mission, you must dispose of empty fuel tanks to keep the aircraft light. If your AI keeps 'ghost copies' of every scene and object you've ever visited, your system will eventually stall and crash. A skilled Navigator audits for **Memory Leaks** and ensures the hangar is cleaned after every flight."

The Concept: Resource Management

As detailed in **Chapter 8**, Unity's Garbage Collector (GC) tries to clean up unused memory, but it can only destroy what it can't "see". If a persistent manager still holds a reference to a deleted object, that object stays in memory forever.

The Cleanup Checklist:
* **Event Unsubscription:** Always use `-=` in `OnDisable` to stop listening to global signals.
* **Resource.UnloadUnusedAssets:** Manually telling Unity to flush the "Hangar" of textures and meshes no longer in use.
* **Nulling References:** Explicitly setting variables to `null` when a mission segment is finished.
Red Flag Detected

The AI Trap: "The Ghost Listener"

You ask the AI: "Make the UI update whenever the MissionManager changes."

// AI-Generated Code: The Memory Leak
public class MissionUI : MonoBehaviour {
    void OnEnable() {
        // Audit Fail: This creates a permanent bond!
        // Even if this UI is destroyed, the MissionManager
        // still tries to send data to it.
        MissionManager.OnUpdate += Refresh; 
    }
}

This is "Dead Telemetry." Over time, the game slows down because it is trying to update 50 "Ghost" UI windows that no longer exist on the screen. A professional Navigator ensures every "Connection" is severed when the hardware is decommissioned.

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 Pilot's Correction

Corrective Protocol
public class MissionUI : MonoBehaviour {
    void OnEnable() => MissionManager.OnUpdate += Refresh;

    // Corrected: Severing the link when the UI is disabled
    void OnDisable() => MissionManager.OnUpdate -= Refresh;

    void Refresh() { /* Update UI logic */ }
}
Your Pilot Command
> A skilled Pilot directs the AI to use Proper Lifecycle Management.
Next Mission
The Navigator's Exam