Interview question C # Memory Management and garbage collection,
Interview question C # Memory Management and garbage collection
How about C # Memory Management?
I have remembered this sentence for more than a week. For more information, see <>. the National Day is idle, so I got to know about it. in fact, I used to contact C # When I was studying C # in my sophomore year, but I was also confused about what I saw. I think vir in C #, by the way, I checked some information to tell the truth and see the headache. After so long, I have learned this for a long time and it is not difficult to come back and see it again. Of course, if I have a deep understanding, I still remember my answer: C # a GC can be recycled by itself, and the reference count is added. That's right. I answered this question in a general sense. Now I think I'm still nervous. I 've been in touch with it before, but I have an impression. Don't worry about the next interview.
. NET resources are divided into two types: managed resources and non-managed resources. Since there are two types of resources, there are naturally two types of recycling methods. Managed resources managed by CLR exist on the managed stack. managed resources are managed by CLR and exist on the managed stack. CLR will call GC for recovery when appropriate. Unmanaged resources:
For example, for resources that cannot be managed by CLR, such as database connections, file handles, and sockets, GC only recycles managed resources and does not recycle unmanaged resources. When Will GC reclaim the memory? When the reference count of an object is 0, CLR calls GC to reclaim the memory. Compared with unmanaged resources, because they are not managed by CLR, We need to manually recycle the memory. Calling the Dispose method is a good choice, but you need to inherit the class from the IDisposable interface, let's take a look at the implementation of this interface:
[ComVisible(true), __DynamicallyInvokable]public interface IDisposable{// Methods[__DynamicallyInvokable]void Dispose();}
I have not learned about Finalize. I will give a link later. You can check it by yourself. We often use the following method when writing database connection statements:
Using (SqlConnection = new SqlConnection (...) {// ADO. NET statement}
This is a standard way of writing. When I was a junior, I learned ado.net. Remember to check the materials and say that this can ensure the correct release of database resources. I didn't think much about it at the time () {} is actually calling dispose to destroy the object, and the object can be destroyed only by inheriting the idispose interface type, and it is try... catch... finally. In this case, let's see if the SqlConnection class has implemented this interface:
[DefaultEvent("InfoMessage")]public sealed class SqlConnection : DbConnection, ICloneable{// Fields}
Look at the DbConnection class:
public abstract class DbConnection : Component, IDbConnection, IDisposable{// Fields}
Indeed, this is true, that is, once the using statement control scope is reached, the dispose method will be automatically called to destroy the object resource. It is said that the close method also called dispose (), look at the source code:
public void Close(){this.Dispose(true);}
Well, if we call the close of ado.net manually, we can also close the database connection and database resources.
Plus: python memory management is also controlled by reference count
I still don't believe that I can solve a broken interview
C # garbage collection mechanism (GC) (conversion)
Interview C # -- when will the Garbage Collector recycle?
C # garbage collection