GC (garbage collection mechanism) in ASP.

Source: Internet
Author: User

1. Get out of the discussion in the group, first record some of the knowledge about GC



Build a test project.


Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;            Namespace gc_test{class Program {static void Main (string[] args) {//Managed code. Code that is managed by the CLR            unmanaged code. Code that is not managed by the CLR//application domain://variables allocated in the stack space once the variables are in the scope of their execution, the variable is immediately recycled by the CLR.            Objects allocated in the heap when no variables refer to it, the object is marked as "junk", waiting for the garbage collector to reclaim it.            The GC periodically cleans up garbage objects in the heap space.            How often the GC cleans up garbage objects programmers cannot decide that the CLR will automatically control.            When 1 objects are marked as "garbage," the object is not necessarily immediately recycled. Gc. Getgeneration (P); Gets the generation//GC where the specified object resides.             Maxgeneration; return algebra. Gc. Collect ();            Immediately allow the garbage collector to recycle all generations. Gc. Collect (int gen);            Garbage collection is immediately performed on the specified generation.            Person p = new person (); Console.WriteLine ("The Generation where the P object resides:" +GC.                      Getgeneration (p)); Gc.            Collect (); Console.WriteLine ("P object's Generation:" + GC.)                   Getgeneration (p)); Gc.            Collect (); Console.WriteLine ("P object's Generation:" + GC.)        Getgeneration (p));           Gc.            Collect (); Console.WriteLine ("P object's Generation:" + GC.)                     Getgeneration (p)); p = null;//When the object is specified as NULL, the GC is immediately reclaimed by the GC.            Collect (); Console.WriteLine ("The largest algebra in the garbage collector at this point is:" + GC.)                Maxgeneration);        Console.readkey (); }    }}



This is the person class used above.

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace gc_test{    class person    {/        /destructor//cannot have an access modifier cannot have parameters        //The destructor is automatically called by GC when the object is reclaimed by the garbage collector.        When performing some cleanup operations,        ~person ()        {            Console.WriteLine ("I am the destructor: when you see me, I am recycled by GC.") ");        }    }}


2. Self-understanding

. NET garbage collection mechanism is automatic, when the object in the program is not referenced, will be marked as "garbage", and then the GC will be timed to search for cleanup, generally without the programmer to manually intervene.

GC garbage is algebraic, divided into three generations, the first garbage will be put into the No. 0 generation mark, when the cleaning time, will automatically identify the need to clean up, and then do not need to be cleaned into the 1th generation, the next time to clean up, or will be identified need to be cleaned and do not need to clean up, Put in the 2nd generation that you don't need to be cleaned up, and clean it up.


When an object is manually specified as NULL, it tells the GC that it can be cleaned up, and the GC cleans up the object the next time it cleans up.


The destructor allows you to see that the object being cleaned is executed. When the garbage collection mechanism cleans an object, it automatically calls the destructor for the object, and you can use this destructor to know that the object is being cleaned up.


3. Running Results


This result occurs two times in the generation: 2, because the first time the object is not specified as NULL, does not immediately clean it, the GC will automatically process it, and the second specifies that the object is null, so that the GC will immediately clean up the object, and then execute the destructor.


4. Exchange Learning


Welcome to the. NET Technology Exchange Group

GC (garbage collection mechanism) in ASP.

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.