- C# 7 and .NET Core 2.0 High Performance
- Ovais Mehboob Ahmed Khan
- 303字
- 2021-08-27 18:47:13
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.
- 同步:秩序如何從混沌中涌現(xiàn)
- 程序員修煉之道:從小工到專家
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- Python數(shù)據(jù)分析入門:從數(shù)據(jù)獲取到可視化
- Oracle RAC 11g實(shí)戰(zhàn)指南
- 數(shù)據(jù)庫開發(fā)實(shí)踐案例
- 大數(shù)據(jù)算法
- Hadoop大數(shù)據(jù)實(shí)戰(zhàn)權(quán)威指南(第2版)
- Mockito Cookbook
- 數(shù)據(jù)驅(qū)動(dòng)設(shè)計(jì):A/B測(cè)試提升用戶體驗(yàn)
- 數(shù)字媒體交互設(shè)計(jì)(初級(jí)):Web產(chǎn)品交互設(shè)計(jì)方法與案例
- 一本書講透Elasticsearch:原理、進(jìn)階與工程實(shí)踐
- Mastering ROS for Robotics Programming(Second Edition)
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)技術(shù)
- Filecoin原理與實(shí)現(xiàn)