Effective C # Chapter II:. NET resource Management

Source: Internet
Author: User
Tags garbage collection resource

A simple fact:. NET applications are running in a managed environment, and this environment has a lot of conflict with different designers, and that's effective C #. A great deal of discussion about the benefits of this environment requires changing your thinking about the localized environment to the. Net CLR. It means to understand. NET garbage collector. It is necessary to be right when you understand what is recommended in this chapter. NET memory management environment has an approximate understanding. Let's start to get a rough idea of it.

The garbage collector (GC) controls managed memory for you. Unlike the local running environment, you don't have to be responsible for memory leaks, indefinite pointers, uninitialized pointers, or a service problem with other memory management. But the garbage collector is not a myth: you have to clean up yourself. You are responsible for unmanaged resources, such as file handles, data links, GDI + objects, COM objects, and other system objects.

Here's the good news: Because GC manages memory, a clear design style can be implemented more easily. Circular references, whether simple or complex, are very easy. GC tags and rigorous, efficient algorithms can detect these relationships and completely delete unreachable Web objects. COM is done by determining whether an object can be reached by "roaming" the tree structure, starting from the root object of the application, rather than forcing each object to maintain some reference tracking. The DataSet is a good example of how the algorithm simplifies and determines the affiliation of an object. A DataSet is a collection of DataTable, and each DataTable is a collection of DataRow, each DataRow is a collection of DataItem, Datacolum defines these types of relationships. Here are a few references to the columns from DataItem to it. At the same time, Datatime also has a reference to its container, that is, DataRow. DataRow contains references to the DataTable, and finally each object contains a reference to the dataset.

The author here is to say: You see, such a complex reference relationship, GC can be easily done, you see the GC is not very strong? )

If this is not complicated enough, you can create a DataView that provides sequential access to filtered data tables. These are managed by DataViewManager. All these references through the Web page form a dataset. Freeing memory is the responsibility of the GC. Because. NET Framework designers so that you do not have to release these objects, these complex Web object references do not cause problems. There is no need to care about the proper release order of these Web objects, which is the work of GC. The design structure of the GC simplifies these issues, and it recognizes that these Web objects are garbage. After the application has finished referencing the dataset, no one can refer to its child objects (the objects in the dataset are no longer referenced). Therefore, there is no object loop reference Dataset,datatables in the Web page is not important, because these objects in the application can not be accessed, they are garbage.

The garbage collector runs on its own thread to remove unused memory from your program. It also compresses the managed heap each time it is run. A compressed heap is the movement of objects in the managed heap together so that contiguous memory can be vacated. Figure 2.1 shows two snapshots of memory that are not garbage collected. All of the free memory will continue after the garbage collection.

Figure 2.1 The garbage collector not only moves unused memory, but also removes other objects, compressing the memory used and freeing up the most free memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.