C # GC garbage collection principles

1. What is a resource:
the so-called resource is a program available data, such as: strings, images, and any binary data, including any type of file.


2. To access the resource:
1) allocate memory: allocate some memory space.
2) initialize the memory: a configuration example of the type responsible for such initialization.
3) use of resources: the use of resources by type of access to members. It will be repeated as needed.
4) the destruction of resources: to perform cleanup work.
What is a managed resource, unmanaged resources: resources are hosted by the CLR solely responsible for resources, resource-bit CLR is not responsible for unmanaged resources.
For managed resources automatically cleans recovered by GC. For unmanaged resources, the code calls manually cleared through, and then recovered by the GC.
How to properly release resources: resources for these operating systems unmanaged resources, is the general, Stream (stream), database connections, network connections, etc., we need to manually released.

Net release provides three methods: Dispose, Close, destructor (i.e. Finalize method)

The first: to provide Close method:
Description: Close Object Explorer, is called upon to show the call.
Close What does it mean that it will not release resources, completely determined by the class designer. Because the Close method does not exist. You do not write do not. So why add a Close method it? In order to avoid unfamiliar with the release of resources more intuitive syntax C # developers, thus providing a Close method. Close method only provides a more consistent for other languages (e.g., C ++) specification. Normally Close method which will call Dispose () method.

The second: Dispose
inherits IDisposable interface, the Dispose method;
Introduction: Dispose method is called, destroy objects, you need to show the call or by using the statement, to be called upon to show the call or leaving the using block.
Memory Dispose method for objects that encapsulate unmanaged resources to clean up, rather than the release of the object, memory object is still controlled by the garbage collector.
Dispose method is called, not only release unmanaged resources that class, but also the release of the unmanaged resource references.
Dispose pattern is a kind of forced convention of Resources to be observed; Dispose pattern IDisposable interface implemented such that the type providing a public Dispose method.
Question 1: How to clean up in the end Dispose internal resources?

Third: destructor (ie Finalize method)
class is a normal situation can not write the destructor, and once wrote a class destructor, it means that GC will call the class in uncertain times destructor, the resource needs to be released is determined whether the class, and calls the finalize method, if the override finalize methods overridden finalize method is called.
Finalize method is to ensure that the role of .NET objects can eliminate unmanaged resources when garbage collection.
In .NET, Object.Finalize () method is not overloaded, the compiler automatically generates a Object.Finalize The class destructor () method
finalize is called by the garbage collector; Dispose invoked by the object.
finalize need to worry because there is no call to finalize the unmanaged resources are not released because GC will call in an uncertain time, of course, you can also call finalize method manually, and dispose must be manually invoked.
finalize Although no need to worry because there is no call to finalize the unmanaged resources are not released, but because management by the garbage collector, can not guarantee the immediate release unmanaged resources; and dispose call it a release unmanaged resources.
Only class types can be rewritten finalize, while the structure can not; and class structure can achieve IDispose reasons see Finalize () characteristics.

Although you can manually release unmanaged resources, we still have to release unmanaged resources in the destructor, it is the only secure applications. Otherwise, if the programmer inadvertently forgot to manually release unmanaged resources, it will bring disastrous consequences. So release unmanaged resources in the destructor, it is a measure of a remedy, at least for most classes is so. 
Since calling the destructor will result in reduced efficiency of recovery GC object, so if you have completed the destructor to do things (such as the release unmanaged resources), should be used to tell GC method SuppressFinalize not need to perform an object destructor. 
Destructor releases only unmanaged resources can not be managed any objects / resources operate. Managed resource because you can not predict the timing of destructor run, so when destructor is executed, maybe you operate has been released. This will lead to serious consequences. 
Overriding the Finalize is not legitimate in the structure, because the structure is a value type, not on the heap, the garbage collector calls Finalize to clean up the managed heap, but not in the structure of the heap.
With class destructor life cycle becomes long. Need twice the memory space garbage collection will not be released, resulting in performance degradation.
A class with a destructor, it refers to many other classes, this will result in these classes have risen to the first generation. (Gc have three generations 0,1,2 recovery mechanisms).

5) release memory: the managed heap memory by the GC solely responsible, the value will be referenced memory on the stack with the demise of stack space and disappear automatically.
GC.Collect (); // for all generations instant force garbage collection
when the amount of memory used on the point of application code to determine a significant reduction, using methods GC.Collect may be appropriate in this case.

Question 2:
Principle garbage collection? Mandatory garbage collection is how the same thing?

 

3, the release pattern: a Microsoft recommended wording, to free up resources to manually display, if you forget, let finalize the release of resources. If the call dispose, the destructor will not call (with call cancellation method SuppressFinalize destructor).

 

Other:
If a reference type object is no longer needed, the need for explicit = null; the answer is: not even do that, GC will be garbage collected.

Published 224 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/xulong5000/article/details/105073952