# Memory Management & GC (.NET) > Garbage Collection, รุ่น (generations), การ finalization, IDisposable, using statement, การรั่วไหลของหน่วยความจำ, การ profiling - 22 คำถามสัมภาษณ์ - Senior - [คำถามสัมภาษณ์: .NET](https://sharpskill.dev/th/technologies/dotnet/interview-questions.md) ## 1. Garbage Collector (GC) ใน .NET คืออะไร? **คำตอบ** Garbage Collector คือกลไกการจัดการหน่วยความจำแบบอัตโนมัติที่คืนหน่วยความจำของออบเจ็กต์ที่ไม่ได้ใช้งานบน managed heap มันทำงานโดยอัตโนมัติในเบื้องหลังเพื่อป้องกันการรั่วไหลของหน่วยความจำ ต่างจาก C/C++ ที่ต้องคืนหน่วยความจำด้วยตนเอง GC จะวิเคราะห์การอ้างอิงของออบเจ็กต์และเก็บกวาดออบเจ็กต์ที่ไม่สามารถเข้าถึงได้จาก root ของแอปพลิเคชันอีกต่อไป ## 2. Garbage Collector ใน .NET มีสามรุ่น (generations) อะไรบ้าง? **คำตอบ** GC ของ .NET ใช้สามรุ่น: Gen0 (ออบเจ็กต์ใหม่ที่มีอายุสั้น เก็บกวาดบ่อย), Gen1 (ออบเจ็กต์อายุปานกลาง เป็นเขตกันชนระหว่าง Gen0 และ Gen2) และ Gen2 (ออบเจ็กต์อายุยาว เก็บกวาดไม่บ่อยเพราะมีต้นทุนสูง) ระบบแบบแบ่งรุ่นนี้ช่วยเพิ่มประสิทธิภาพโดยเก็บกวาดออบเจ็กต์อายุสั้น (Gen0) ที่คิดเป็น 80-90% ของออบเจ็กต์ก่อน ในขณะที่หลีกเลี่ยงการสแกนออบเจ็กต์ที่คงอยู่นาน (Gen2) บ่อยครั้ง ## 3. Large Object Heap (LOH) ใน .NET คืออะไร? **คำตอบ** Large Object Heap คือพื้นที่หน่วยความจำแยกต่างหากสำหรับออบเจ็กต์ขนาด 85 KB ขึ้นไป ซึ่งในเชิงตรรกะอยู่ใน Gen2 ต่างจาก heap มาตรฐานที่ถูก compaction เป็นประจำ LOH จะไม่ถูก compaction โดยค่าเริ่มต้นเพื่อหลีกเลี่ยงต้นทุนในการคัดลอกออบเจ็กต์ขนาดใหญ่ ซึ่งอาจทำให้เกิดการแตกกระจาย (fragmentation) ของหน่วยความจำ ตั้งแต่ .NET 4.5.1 เป็นต้นมา สามารถเปิดใช้งานการ compaction ของ LOH ด้วยตนเองได้ด้วย GCSettings.LargeObjectHeapCompactionMode ## มีอีก 19 คำถาม - บทบาทของ pattern IDisposable ใน .NET คืออะไร? - คำสั่ง using ใน C# ทำหน้าที่อะไร? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ .NET อื่นๆ - [พื้นฐาน C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-basics.md): 25 คำถาม, Junior - [LINQ & Delegates](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-linq-delegates.md): 20 คำถาม, Junior - [พื้นฐานสำคัญของภาษา C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-language-essentials.md): 15 คำถาม, Junior - [พื้นฐาน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/aspnet-core-fundamentals.md): 18 คำถาม, Junior - [วงจรชีวิตของ request ใน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/aspnet-core-request-lifecycle.md): 20 คำถาม, Junior - [Configuration & Settings](https://sharpskill.dev/th/technologies/dotnet/interview-questions/configuration-settings.md): 20 คำถาม, Junior - [วงจรชีวิตของแอปพลิเคชัน](https://sharpskill.dev/th/technologies/dotnet/interview-questions/application-lifecycle.md): 20 คำถาม, Junior - [Dependency Injection](https://sharpskill.dev/th/technologies/dotnet/interview-questions/dependency-injection.md): 24 คำถาม, Mid-Level - [Entity Framework Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/entity-framework-core.md): 25 คำถาม, Mid-Level - [Minimal APIs](https://sharpskill.dev/th/technologies/dotnet/interview-questions/minimal-apis.md): 18 คำถาม, Mid-Level - [การพัฒนา Web API](https://sharpskill.dev/th/technologies/dotnet/interview-questions/web-api-development.md): 22 คำถาม, Mid-Level - [Async ใน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/async-aspnet-core.md): 20 คำถาม, Mid-Level - [Authentication & Authorization](https://sharpskill.dev/th/technologies/dotnet/interview-questions/authentication-authorization.md): 18 คำถาม, Mid-Level - [HttpClient & Networking](https://sharpskill.dev/th/technologies/dotnet/interview-questions/httpclient-networking.md): 20 คำถาม, Mid-Level - [การซีเรียลไลซ์ JSON](https://sharpskill.dev/th/technologies/dotnet/interview-questions/json-serialization.md): 20 คำถาม, Mid-Level - [Entity Framework Core ขั้นสูง](https://sharpskill.dev/th/technologies/dotnet/interview-questions/ef-core-advanced.md): 25 คำถาม, Mid-Level - [ฟีเจอร์ขั้นสูงของ C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-advanced-features.md): 20 คำถาม, Mid-Level - [Clean Architecture](https://sharpskill.dev/th/technologies/dotnet/interview-questions/clean-architecture.md): 20 คำถาม, Mid-Level - [Logging, Monitoring & Observability](https://sharpskill.dev/th/technologies/dotnet/interview-questions/logging-monitoring.md): 22 คำถาม, Mid-Level - [Unit Testing และ xUnit](https://sharpskill.dev/th/technologies/dotnet/interview-questions/unit-testing-xunit.md): 20 คำถาม, Mid-Level - [การทดสอบแบบบูรณาการ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/integration-testing.md): 18 คำถาม, Mid-Level - [Docker & Containerization](https://sharpskill.dev/th/technologies/dotnet/interview-questions/docker-containerization.md): 16 คำถาม, Mid-Level - [การจัดการแพ็กเกจ NuGet](https://sharpskill.dev/th/technologies/dotnet/interview-questions/nuget-package-management.md): 16 คำถาม, Mid-Level - [การเขียนโปรแกรมแบบรีแอกทีฟ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/reactive-programming.md): 24 คำถาม, Senior - [Async/await และรูปแบบ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/async-await-patterns.md): 26 คำถาม, Senior - [.NET Design Patterns](https://sharpskill.dev/th/technologies/dotnet/interview-questions/dotnet-design-patterns.md): 24 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/dotnet/interview-questions/performance-optimization.md): 22 คำถาม, Senior - [ความปลอดภัยและ Best Practices](https://sharpskill.dev/th/technologies/dotnet/interview-questions/security-best-practices.md): 24 คำถาม, Senior - [SignalR และเรียลไทม์](https://sharpskill.dev/th/technologies/dotnet/interview-questions/signalr-realtime.md): 22 คำถาม, Senior - [สถาปัตยกรรม Microservices](https://sharpskill.dev/th/technologies/dotnet/interview-questions/microservices-architecture.md): 25 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/dotnet/interview-questions/memory-management-gc