C# 手動釋放記憶體實驗

C# 手動釋放記憶體實驗

C# 手動釋放記憶體實驗


資料來源:

https://docs.microsoft.com/zh-tw/dotnet/api/system.gc.collect?redirectedfrom=MSDN&view=netframework-4.8#overloads
https://blog.csdn.net/lastBeachhead/article/details/3379230


GITHUB: https://github.com/jash-git/CS_Console_Release_STACK


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

namespace Console_Release_STACK
{
    //C# 手動釋放記憶體實驗
    //https://docs.microsoft.com/zh-tw/dotnet/api/system.gc.collect?redirectedfrom=MSDN&view=netframework-4.8#overloads
    //https://blog.csdn.net/lastBeachhead/article/details/3379230

    class MyGCCollectClass
    {
        private const int maxGarbage = 1000;
        static void pause()
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey(true);
        }
        static void Main(string[] args)
        {
            int count = 0;
            Console.WriteLine("count={0}", count++);
            Console.WriteLine("Memory used before collection:       {0:N0}",
                              GC.GetTotalMemory(false));
            // Put some objects in memory.
            MyGCCollectClass.MakeSomeGarbage();

            Console.WriteLine("count={0}", count++);
            Console.WriteLine("Memory used after collection:       {0:N0}",
                              GC.GetTotalMemory(false));

            // Collect all generations of memory.
            GC.Collect();
            Console.WriteLine("count={0}", count++);
            Console.WriteLine("Memory used after full collection:   {0:N0}",
                              GC.GetTotalMemory(false));
            Console.WriteLine("count={0}", count++);
            pause();
        }

        static void MakeSomeGarbage()
        {
            Version vt;

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

5 thoughts on “C# 手動釋放記憶體實驗

    1. JAVA
      記憶體
      回收
      變數
      垃圾
      ———————-

      有文章指出在變數確定不使用時,只接設定NULL

      另外也可以執行
      System.gc(); // 建議回收物件

        1. //JAVA ONELINE 測試

          //https://www.tutorialspoint.com/online_java_compiler.php

          //確定 System.gc(); 和 指定NULL 豆JAVA都是參考用

          public class GcTest {
          private String name;
          public GcTest(String name)
          {
          this.name = name;
          System.out.println(name + “Create”);
          }
          protected void finalize()
          {
          System.out.println(name + “Delete”);
          }
          }
          public class HelloWorld{

          public static void main(String []args){
          System.out.println(“Hello World…000”);
          GcTest obj1 = new GcTest(“object1…”);
          GcTest obj2 = new GcTest(“object2…”);
          GcTest obj3 = new GcTest(“object3…”);
          System.out.println(“Hello World…001”);

          obj1 = null;
          obj2 = null;
          obj3 = null;
          System.out.println(“Hello World…002”);
          System.gc();
          System.out.println(“Hello World…003”);

          }
          }
          /*
          Hello World…000
          object1…Create
          object2…Create
          object3…Create
          Hello World…001
          Hello World…002
          Hello World…003
          object1…Delete
          object3…Delete
          object2…Delete
          */

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *