.NET จัดการหน่วยความจำ โดยอัตโนมัติ ผ่าน garbage collector (GC) — มันจองอ็อบเจกต์บน managed heap และเรียกคืนอ็อบเจกต์ที่เข้าถึงไม่ได้อีกต่อไป ดังนั้นคุณจึงไม่ต้องคืนหน่วยความจำเอง การเข้าใจ GC (แบบ generational, การแบ่ง stack/heap และ IDisposable) มีความสำคัญต่อประสิทธิภาพและความถูกต้อง
stack กับ heap
Stack → value types (locals), method frames; fast, automatically freed when scope ends.
Heap → reference type objects (class instances); managed by the GC.
A reference variable lives on the stack but points to its object on the heap.
