Garbage What is Collector (GC)?
Garbage Collector collects objects that are no longer used in memory. It is a memory management tool that cleans automatically. While the application is running, Dynamically created objects fill memory space over time and They continue to take up space in memory even when they are not used. GC, this is now By detecting and cleaning objects, it frees memory and improves application performance. protects.
Garbage How Does Collector Work?
Garbage Collector runs in the background while .NET Core applications are running It works automatically in the plan. Its operation takes place in three main stages:
1.< span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-size-adjust: none; font-kerning: auto; font-optical-sizing: auto; font-variation-settings: normal; font-variant-emoji: normal; normal;"> Identification of Roots: Objects that the application can access are determined. These roots come from memory Indicates objects that should not be deleted.
2.< span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-size-adjust: none; font-kerning: auto; font-optical-sizing: auto; font-variation-settings: normal; font-variant-emoji: normal; normal;"> Marking: GC scans all objects except roots and detects which ones are not used. determines.
3.< span style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-size-adjust: none; font-kerning: auto; font-optical-sizing: auto; font-variation-settings: normal; font-variant-emoji: normal; normal;"> Memory Clearing: Objects that are no longer used are cleared from memory and this space is It is available for objects.
.NET Core How to Use Garbage Collector Effectively in MVC?
Garbage Collector' To manage the operation of the application correctly, can improve performance. Here are some tips:
1. Manage the Life of Objects Correctly: Long-lived objects are larger than short-lived objects It is costly. AddScoped< /b>, AddTransient, and AddSingleton.
2. Dispose Usage: Manually releasing objects by implementing the IDisposableinterface It is possible to quit. This makes the GC work less.
3. Get to Know Garbage Collection Modes: Workstation GC (single processor) and Server in .NET Core MVC There are two GC modes: GC (multiprocessor). Server GC, high It is more efficient in applications requiring performance.
In the example below, manually created objects You can see how it can be freed from memory with the Dispose method:
public class FileManager : IDisposable { private FileStream fileStream; public FileManager(string path) { fileStream = new FileStream(path, FileMode.Open); } public void Dispose() { fileStream?.Dispose(); } } |
In this example, FileManager class implements the IDisposableinterface and we saw that it released the FileStreamobject with the Disposemethod.

GC's Impact on Performance
Garbage Collector can speed up the application, but it It may have adverse effects when it works. So it's about memory usage It is important to choose the correct GC mode by following the data. For example, young generations (Gen 0) clear quickly, but older generations (Gen 1 and Gen 2) takes longer to clear. Using this information, memory You can manage the density.
.NET Garbage Collector usage, memory in Core MVC It is an effective way to optimize management. How GC works Knowing this, you can improve your app's performance and improve user experience. can heal. Understanding GC and using it correctly is the basis of writing performance-friendly code. It lies in managing it properly.