C # GC destructor and the garbage collector and the use of IDisposable

First, what is GC

1, GC is a garbage collector, in general, the system will automatically detect objects or variables will not be used to release the memory, do not need to manually call with Collect () is mandatory garbage collection, the timely release of memory, so the program more efficient.

2, GC: only responsible for the recovery of managed objects, is not responsible for the recovery of non-managed objects.

What is spam? Full access to garbage is not something, that is the object or value are not referenced after the current program execution

As shown below:

code show as below:

the using the System;
 the using the System.Collections.Generic;
 the using the System.Linq;
 the using the System.Text;
 the using System.Threading.Tasks; 

namespace TestDemo 
{ 
    public  class TestDemo 
    { 
        Private  static the Test T = new new the Test () // static and can not be recovering static hold references will not be recovered 
        { 
            Id = 123 , 
            the Name = " the Test " 
        }; 
        public  static  void Show () 
        { 
            ///The braces {} is the code block means, and does not affect between two blocks, the first block after the implementation is generally active will be released GC
             /// The reason for this obj is not released because static traversal of the reference obj of type variables 
            {
                 Object obj = new new {the Name = . 1 }; 
                t.obj = obj;
                 int I = . 3 ; // will be GC 
                TestDemo testDemo = new new TestDemo (); // will be GC 
            } 
            { 
                the GC.Collect (); // active the GC 
            } 
        } 
    } 

    public  class  the Test
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public object obj { get; set; }
    }
}

Summary: The program will be executing the garbage, this is still a static obj t use, it has not been released

How that GC recycling it?
1, new memory is not enough time to go through all the found object heap, marking not visit, and then start a thread to clean up the memory
2, a clear mark of the object, the other to move, and then neatly placed, so this time the entire thread stops is not allowed to operate the memory
3, not enough memory refers to a memory object, there is a critical value, nor is the size of the entire heap

GC execution of the recovery process:
1, before the first GC All objects are 0
objects 2, after the first GC, also called retention level 1
3 0 objects recovered to go first, if space is not enough, then go to 1 level objects, after that, there's the object becomes level 2
4 0 is not enough, not enough level 1, level 2 is not enough, then memory overflow
5, the more recent assignment, the more will be recycled, such as The for loop to create objects

Large objects and normal object cache addresses are not the same.
Large Objects strategy: if the object is greater than a certain value of 85k, managed separately, using a list (fragments), avoid frequent memory mobile

Second, the destructor and IDisposable difference?

~ Class () destructor: mainly used to release resources unmanaged, the waiting time to the GC unmanaged resources freed performed automatically
when GC recovered, the CLR certain calls, but may be delayed (the object does not release know how long it)

Dispose (): Releases the unmanaged resources also take the initiative to release, the method itself is meaningless, we need to achieve the release of the resources which method
GC does not call, but when using objects, users take the initiative to call this method, go to releases unmanaged resources,
rather than that when the object is released, it will go automatically call the Dispose method, and then run out of the object at the time, we take the initiative to perform the dispose method, of course, may be using a shortcut

The following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDemo
{
    public class TestDemoThree
    {
        public static void show()
        {
            {
                for (int i = 0; i < 2; i++)
                {
                    TestThree @class = new TestThree()
                    {
                        ID=I, 
                        the Name = " TestThree " 
                    }; 
                } 
            } 
            { 
                the GC.Collect (); // active GC when destructor CLR certain calls 
            } 
            { 
                /// after using this would call to call Dispose, equivalent to try of the finally 
                the using (TestThree TestThree = new new TestThree () 
                { 
                    Id = . 1 , 
                    the Name = " 444 " 
                }) 

                    the try 
                    { 
                        //using相当于
                    }
                    finally
                    {
                        //调用的dispose()
                    }
            }
        }

        public class TestThree : IDisposable
        {
            public int Id { get; set; }
            public string Name { get; set; }
            ~TestThree()
            {
                Console.WriteLine($"执行{this.GetType().Name}~TestThreeDispose");
            }
            public void Dispose()
            {
                Console.WriteLine($"执行{this.GetType().Name}Dispose");
            }
        }
    }
}

The results of the implementation of the initiative GC

 using the results

 

Guess you like

Origin www.cnblogs.com/May-day/p/11398776.html