"C #" GC and destructor (Finalize method)

Source: Internet
Author: User
Tags mscorlib

Destructors:

(from Baidu Encyclopedia) the destructor (destructor), in contrast to the constructor, automatically executes the destructor when the object is detached from its scope (for example, the function where the object is already called). Destructors are often used to "clean up" work (for example, to open up a memory space with new when creating objects, and to release them in a destructor before exiting).

Destructors in C # define a method similar to C + +, ~+ function names:

1      Public class Finalizeclass 2     {3         ~finalizeclass ()4        {             5   In this case, clean up the unmanaged resource 6        }7     }

The generated IL code:

1.class  PublicAuto ansi BeforeFieldInit test.finalizeclass2 extends [mscorlib]System.Object3 {4     //Methods5. Method family HidebysigVirtual 6InstancevoidFinalize () cil managed7     {8         //Method begins at RVA 0x20709         //Code Size (0x19)Ten. maxstack1 One  A.Try -         { - Il_0000:nop theIl_0001:ldstr"the destructor of Finalizeclass" -Il_0006:callvoid[mscorlib] System.console::writeline (string) - Il_000b:nop - Il_000c:nop + IL_000D:LEAVE.S il_0017 -}//end. Try +         finally A         { atIl_000f:ldarg.0 -Il_0010:call instancevoid[Mscorlib]system.object::finalize () - Il_0015:nop - il_0016:endfinally -}//End Handler -  in Il_0017:nop - Il_0018:ret to}//end of Method Finalizeclass::finalize +  -. method Publichidebysig specialname rtspecialname theInstancevoid. ctor () CIL managed *     { $         //Method begins at RVA 0x20a8Panax Notoginseng         //Code size 7 (0x7) -. maxstack8 the  +Il_0000:ldarg.0 AIl_0001:call instancevoid[Mscorlib]system.object::.ctor () the Il_0006:ret +}//end of Method Finalizeclass::.ctor -  $}//end of class Test.finalizeclass

Actually generates a finalize method that internally invokes the Base.finalize () method, which is the Finalize method of object.

The Finalize method can only be called by the GC and we cannot invoke it. The following is the GC call Finalize process!

Finalization list (Queue) (end list)

We new an object, if the object contains a finalize method, after the memory is opened, a pointer to it will be placed in the end list (except for object objects).

Freachable Queue (f-reachable end reachable queue)

When garbage collection starts, objects that are judged to be garbage (unreachable) are removed from the finalization list and stored in the freachable queue if they exist in the finalization list. At the same time, these objects become reachable and will not be recycled by GC, which means that these objects are promoted to another generation, which is assumed to be a 2-generation object.

The objects in the queue are reachable, and the Finalize method needs to be executed. The Finalize method is executed by a high-priority CLR thread, and after execution, the object's pointer is removed from the freachable queue (when the queue is empty, the thread sleeps and is awakened when it is not empty).

When garbage collection is done again, the objects in the original freachable queue are processed into unreachable objects (2 generations), and the object memory is actually freed only if the 2 generation of memory is not available for garbage collection of the 2 generation objects. Therefore, the object containing the Finalize method must be garbage collected at least two times before it can be truly released.

See diagram:

Objects 2, 3, 5, 6, 10 contain the Finalize method, 2, 5, 7, and 9 are unreachable objects (the target of the GC).

When GC is performed, because 2, 5 objects contain a Finalize method, they are placed in the freachable queue, become reachable objects, and ascend generations without garbage collection. and objects 7, 9 are recycled directly.

If the original freachable is in the GC, the memory of object 2 and 5 is reclaimed.

Conclusion

1. Objects containing a Finalize method must be collected at least two times before they are actually released.

2. If it is not necessary, it is not recommended to define a Finalize method (override with Dispose mode).

"C #" GC and destructor (Finalize method)

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.