記憶體回收與徹底關閉表單方法

 

針對記憶體回收與徹底關閉表單方法有幾個主題與要點如下

一、GC


using System;

namespace GCCollectIntExample
{
    class MyGCCollectClass
    {
        private const long maxGarbage = 1000;

        static void Main()
        {
            MyGCCollectClass myGCCol = new MyGCCollectClass();

            // Determine the maximum number of generations the system
            // garbage collector currently supports.
            Console.WriteLine("The highest generation is {0}", GC.MaxGeneration);

            myGCCol.MakeSomeGarbage();

            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            // Determine the best available approximation of the number 
            // of bytes currently allocated in managed memory.
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            // Perform a collection of generation 0 only.
            GC.Collect(0);
            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));

            // Perform a collection of all generations up to and including 2.
            GC.Collect(2);
            // Determine which generation myGCCol object is stored in.
            Console.WriteLine("Generation: {0}", GC.GetGeneration(myGCCol));
            Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(false));
            Console.ReadKey();
        }

        void MakeSomeGarbage()
        {
            Version vt;

            for (int i = 0; i < maxGarbage; i++)
            {
                // Create objects and release them to fill up memory
                // with unused objects.
                vt = new Version();
            }
        }
    }
}

1、上面範例表示generational collection有三層(0~2)

2、每做一次GC.Collect會把還存活的程式記憶體位置移往下一層

3、GC.collect會暫停所有目前正在執行的執行緒,太常呼叫會有效能低落問題。

 

二、GC.SuppressFinalize


using System;
using System.Data.SqlClient;

namespace Samples
{
    public class DatabaseConnector : IDisposable
    {
        private SqlConnection _Connection = new SqlConnection();

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_Connection != null)
                {
                    _Connection.Dispose();
                    _Connection = null;
                }
            }
        }
    }
}

GC.SuppressFinalize的功能為讓程式不會對已記憶體回收的物件重複再做回收動作

 

三、Dispose與Finalize

Dispose:表示此資源被標記為待釋放

參考實作 Dispose 方法IDisposable 介面實作 Finalize 和 Dispose 以清除 Unmanaged 資源

 

四、列出相關VB.NET記憶體回收與關閉保單的方法

  1. GC.Collect()
  2. Me.Dispose()
  3. Me.Close()
  4. System.Windows.Forms.Application.Exit()
  5. System.Windows.Forms.Application.ExitThread()
  6. System.Environment.Exit(Environment.ExitCode)

 

參考資料:

C# - 資源釋放的觀念整理

.NET 的自動記憶體管理

使用封裝資源的物件