. NET managed resource unmanaged resource garbage collection query

Source: Internet
Author: User
Tags garbage collection resource

The CLR provides developers with a very exciting feature-garbage collection. But the garden's discussion of garbage collection is mostly about the principle of garbage collection and the Dispose model. However, garbage collection in the actual use, can not achieve its design goals, in the development process, there is no need to pay attention to the problem? I am not very clear, this article hope to be able to achieve the effect of a good, I hope that individual cattle can give me or the same people who have doubts a clear and clear answer.

What is garbage collection? This means that when you use the CLR (not including managed resources), you need to use only the new object. Without the need to release the object through program code (above is the meaning of garbage collection that I understand).

Managed Resources Unmanaged Resources

The difference between managed and unmanaged resources often makes it unclear to developers exactly what is managed and what is not. Here's a little bit of what you're feeling. If the class you are using belongs to the CLR, no matter what resources it actually uses. The CLR will help you with garbage collection. Simply like a list object, you use completion (the variable is detached from its scope) and are automatically recycled by the CLR. So for the internet often said data connection, file class? It is also automatically recycled after use, but because such resources are important, the timing of garbage collection is uncertain. So many textbooks say that such resources need to be released in time after use (through program code). The point is that the class of the CLR is automatically recycled, regardless of whether it actually controls managed or unmanaged resources.

Here you can do a simple test, a WinForm project, two button (button1, Button2), click the event below.

private void button1_Click(object sender, EventArgs e)

{

FileStream file = new FileStream(@"c:\DisposeTest.txt", FileMode.Open);

}

private void button2_Click(object sender, EventArgs e)

{

GC.Collect();

}

Consecutive clicks Button1, the second time will be an error, the document has not been released. But if you click Button1 and then click Button2 and then click Button1 then there will be no error, because Button2 garbage collection will release the file.

So which resources, objects are not automatically recycled, must be released through the program code? Simply say the class you are using, the component does not belong to the CLR. such as win32api,com components. When you use these types, if you assign resources, you must release them through code. Or a file Open example, if you are using WIN32API to open the file, then using garbage collection is no way to release resources. So be sure to release the related resources in your code.

Problems in the development of garbage collection

Managed resources, unmanaged resources have been clearly stated. The principle of garbage collection here is not said, online a lot of articles introduced. Let's say that in the actual development process can not fully believe garbage collection, and do not need to consider the release of objects? Or what objects need to be released in time? This is primarily engaged in WinForm development. So the following discussion may be based on a WinForm background. At present I am clear or not clear content hope to discuss with you.

1. What resources need to be released in time? This question is clear, database connection, file, and others to add it. Just think so much.

2 WinForm background, if the created form is released (form = null), the fields within the form (for example, String, DataTable, other custom classes) are automatically freed (assuming that they are not referenced elsewhere).

3 because engaged in is the database related development, the natural use most is the ado.net, then besides connection, whether other objects need to release in time, which object whether needs to release in time (here more concerned is DataSet, DataTable)?

4) Add that if the above many objects need to be released in time, then the meaning of garbage collection is not a certain degree of reduction?

The above is the individual to the garbage collection understanding and the question, hoped everybody can discuss together, finally can have the definite conclusion.

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.