Tier 3 Series 202

202A: The Garbage Chute

Pilot Record
Student Profile
"A fighter jet cannot fly if the pilot throws a heavy trash bag out the window every second. The turbulence (Garbage Collection) will eventually stall the engine. In C#, creating temporary strings or classes inside the Update loop creates "Memory Trash" that causes the game to freeze when the cleaner arrives."

The Concept: Garbage Collection (GC)

Memory in C# is managed automatically. When you create a new object (using `new` or string concatenation), it takes up RAM. When you stop using it, it becomes "Garbage." Eventually, Unity pauses your game to clean this up. This pause is called a **GC Spike**.

* **The Rule:** Zero Allocations in Update.
* **The Culprits:** String concatenation (`+`), creating new Lists inside loops, and Boxing.
Red Flag Detected

The AI Trap: "The String Stitcher"

You ask the AI: "Update the score text every frame."

// AI-Generated Code: A Memory Disaster
void Update() {
    // Audit Fail: Creating a NEW string object 60 times a second.
    // "Score: " + 10 = New String in Memory.
    scoreText.text = "Score: " + currentScore;
}

This is "Allocation Bloom." While it looks harmless, string concatenation allocates new memory every single frame. On mobile, this causes the game to stutter every few seconds.

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
// Corrected: Event-Driven Update
private int lastScore = -1;

void Update() {
    // Only allocate memory when data changes
    if (currentScore != lastScore) {
        scoreText.text = $"Score: {currentScore}";
        lastScore = currentScore;
    }
}
Your Pilot Command
> A skilled Pilot directs the AI to use Caching or StringBuilders. You command: "Only update the text if the score actually changed, and consider using a cached string format."
Next Mission
The Object Pool