.NET

Memory Management & GC

Garbage Collection, generations, finalization, IDisposable, using statement, memory leaks, profiling

22 面接問題·
Senior
1

What is the Garbage Collector (GC) in .NET?

回答

The Garbage Collector is an automatic memory management mechanism that frees unused objects on the managed heap. It runs automatically in the background to prevent memory leaks, unlike C/C++ where memory must be manually freed. The GC analyzes object references and collects those that are no longer accessible from application roots.

2

What are the three generations of the Garbage Collector in .NET?

回答

The .NET GC uses three generations: Gen0 (young, short-lived objects, collected frequently), Gen1 (medium lifetime objects, buffer zone between Gen0 and Gen2), and Gen2 (long-lived objects, collected rarely as it's expensive). This generational system optimizes performance by first collecting ephemeral objects (Gen0) which represent 80-90% of objects, while avoiding frequent scanning of persistent objects (Gen2).

3

What is the Large Object Heap (LOH) in .NET?

回答

The Large Object Heap is a separate memory space for objects of 85 KB or larger, logically belonging to Gen2. Unlike the standard heap which is regularly compacted, the LOH is NOT compacted by default to avoid the cost of copying large objects. This can cause memory fragmentation. Since .NET 4.5.1, LOH compaction can be manually enabled with GCSettings.LargeObjectHeapCompactionMode.

4

What is the role of the IDisposable pattern in .NET?

5

What does the using statement do in C#?

+19 面接問題

次の面接に向けて.NETをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める