.NET technology FAQ (5) ------ garbage collection

xiaoxiao2021-03-06  66

5. Garbage collection 5.1 What is garbage collection? Garbage collection is a system that runs the library components through it to manage objects of the object's survival cycle and their occupied stacks. For .NET, it is not a new concept - Java and many other languages ​​/ run libraries have been used for a while. 5.2 After the last reference to the object is canceled, it does not necessarily be destroyed immediately, right? Yes it is. The garbage collector does not provide destruction of objects and is a time guarantee for the memory. 5.3 Why doesn't you provide a sectative destructure? Because of the garbage collection algorithm. The .NET's garbage collector works through the list of all objects that are currently being used. All unrecognized objects during the scan can be destroyed and released. When the last reference to the object is canceled, this implementation of the algorithm makes the running library unable to get notifications - it can only discover when the next cleaning stack. Moreover, this algorithm performs garbage collection as little as possible to work most efficient. Typically, the consumption of the stack capacity triggers the collection process. 5.4 Is there a problem with the lack of determination in .NET? This does affect the design of the component. If your object requires expensive or shortage resources (such as locking the database), you need to provide some way to let the client tell the object after work. Microsoft recommends that you should provide a method called Dispose () for this purpose. However, this will cause problems in a distributed object - who calls the dispose () method in a distributed system? There is a need for some form of reference-counting mechanism or owner management mechanism to handle distributed objects - unfortunately, the run library is loved to this. 5.5 Determination of destructure affects the use of COM objects in managed code? Yes it is. When using COM objects from managed code, you actually rely on garbage collectors to eventually release your object. If your COM object has expensive resources and can only be released after the event is finally released, you may need to provide a new interface on your object to support explicit DISPOSE () methods. 5.6 I have heard that I should avoid using the Finalize method, then should I implement Finalize in my category? For the garbage collector, the object with the Finalize method is more work than the object that does not have this method. It also does not guarantee the order of Finalized, so there is a different view for accessing other objects from a Finalized method. Finally, it cannot guarantee that the finalized method will be called. So, never rely on it to clean up the resources of the object. Microsoft recommends the following way: public class ctest {public override void dispose () {... // cleanup activities gc.suppressFinalize (this);} protected override void finalize () {dispose ();}} Call Dispose (), the resource of the object is released, and the garbage collector is removed from the Finalize obligation by calling suppressFinalize (). In the most unfavorable case, that is, the client has forgotten to call Dispose (), there is a large chance to call Finalize () to finalize the resources of the object via the garbage collector. Due to the defects of the garbage collection algorithm, this looks like a fairly reasonable handling method. 5.7 Do I have a means of controlling garbage collection algorithms? A little. The System.gc class provides a pair of interesting methods. The first is a Collect method - it enforces the garbage collector to immediately collect all untruritable objects. The other is RequestFinalizeOnShutdown (), which tells the garbage collector to run the femalize () method for each object when the application is shut down.

When the application is closed, the garbage collector generally selects a quick exit method rather than calling Finzlize (), so this method can manually force the library to negative a little responsibility. If you want to verify that this is not just theoretical, try the following test procedures: use system; class ctest {protected override void finalize () {console.writeline ("this is the finalizer.");}}

Class capplication {public static void main () {console.writeline ("this is main."); ctest test = new ctest (); // gc.requestfinalizeonseonShutdown ();}} Run this program, then remove GC.RequestFinalizeonShutdown () This line of comments marked and re-run, pay attention to what is different ... How do I know what the garbage collector is doing? Many of the .NET Runturing Base is output through the 'COM MEMORY' performance object. Use the Performance Monitor to view them.

转载请注明原文地址:https://www.9cbs.com/read-119086.html

New Post(0)