Interview Questions C # memory management and garbage collection

Source: Internet
Author: User

Interview Questions C # memory management and garbage collection

What do you say about C # 's memory management?

I remember this sentence for more than a week, since the last northeast Normal University interview, specific please see <> National Day Idle Nothing, probably understand a bit, in fact, sophomore learning C # time contact, but that will look at the ignorant, I see is vir in C #, by the way to check some information, truth, look at the headache. Now after so long, learned so long, and then come back to see the fact is not difficult, of course, in-depth to learn about GG, I remember my answer is: C # has a GC can be recycled, in addition to the reference count. Yes, I answer is so general, now think about or tense, I have contact before, in fact, have the impression that the next interview do not hurry, slowly recalled.

There are two types of resources in. NET: Managed and unmanaged, and since there are two types of resources, there are two ways of recycling. Managed resources, which are managed by the CLR that exist on the managed heap, are managed by the CLR, exist on the managed heap, and the CLR calls the GC for recycling when appropriate. Unmanaged Resources:
such as database connections, file handles, sockets, and other resources that are not managed by the CLR, GC reclaims only managed resources and does not reclaim unmanaged resources. So when does the GC reclaim memory? When an object has a reference count of 0, the CLR calls the GC to reclaim memory. As opposed to unmanaged resources, because it is not managed by the CLR, so we need to recycle the memory manually, calling the Dispose method is a good choice, but you need to inherit the class from the IDisposable interface to see the implementation of this interface:

[ComVisible (true), __dynamicallyinvokable]  Public Interface idisposable{//  Methods[__dynamicallyinvokable]void  Dispose ();

There is a finalize I did not go to understand, probably looked at the next, and so I will give a link, you can see for yourself. We often use a notation when writing database connection statements:

using New SqlConnection (...)) {////ado. NET Statement }

This is a more standard way of writing, the junior high School when learning ADO, remember the data said, this can ensure the correct release of database resources, then did not think, today, the data said, using () {} is actually called Dispose destroy object, And only the type that inherits the IDispose interface can destroy the object, and it is try...catch...finally simplification. In that case, let's see if the SqlConnection class implements this interface:

[DefaultEvent ("infomessage")]  Public Sealed class sqlconnection:dbconnection, icloneable{//  fields}

Look at the DbConnection class:

 Public Abstract class dbconnection:component, IDbConnection, idisposable{//  fields}

Sure enough, this is true, that is, once the control range of the using statement is out, the Dispose method is automatically called to destroy the object resource. It is said that the Close method is also called Dispose () to see the source code:

 Public void Close () {this. Dispose (true);

Well, really, if we manually invoke the close of ADO, it is also possible to close the database connection with the database resource.

Plus:python's memory management is also controlled by reference counting.
I don't believe I can't solve a broken interview.

C # Technology Ramble garbage Collection Mechanism (GC) (GO)

Interview c#--When is the garbage collector recycled?

On C # garbage collection

Interview Questions C # memory management and garbage collection

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.