官术网_书友最值得收藏!

Garbage collection

One of the most important features of CLR is the garbage collector. Since the .NET Core applications are managed applications, most of the garbage collection is done automatically by the CLR. The allocation of objects in the memory is efficiently done by the CLR. The CLR not only tunes the virtual memory resources from time to time, but it also reduces the fragmentation of underlying virtual memory to make it more efficient in terms of space.

When the program is run, the objects start allocating memory on the heap and each object's address is stored on the stack. This process continues until the memory reaches its maximum limit. Then the GC comes into play and starts reclaiming memory by removing the unused managed objects and allocating new objects. This is all done automatically by the GC, but there is also a way to invoke the GC to perform garbage collection by calling the GC.Collect method

Let's take an example where we have a Car object called c in the Main method. When the function is executed, the Car object will be allocated by the CLR into the heap memory and the reference to that c object will be stored in the stack address pointing to the Car object on the heap. When the garbage collector runs, it reclaims the memory from the heap and removes the reference from the stack:

Some important points to note are that the garbage collection is done automatically by the GC on managed objects, and that if there are any unmanaged objects, such as database connections, I/O operations, and so on, they need to be garbage collected explicitly. Otherwise, GC works efficiently on managed objects and ensures that the application will not experience any decrease in performance when the GC is performed.

主站蜘蛛池模板: 津市市| 新绛县| 宁远县| 通河县| 雅安市| 盈江县| 巴东县| 右玉县| 新兴县| 家居| 湾仔区| 济阳县| 通化县| 江源县| 邯郸市| 阜新| 古田县| 临沂市| 鄂温| 乾安县| 固镇县| 娄底市| 松桃| 建平县| 石景山区| 天水市| 三门峡市| 彭山县| 定结县| 卢氏县| 呼图壁县| 城固县| 彝良县| 塔河县| 海兴县| 唐河县| 静海县| 长宁县| 海阳市| 巴彦淖尔市| 夹江县|