{"id":11571,"date":"2025-09-21T10:49:07","date_gmt":"2025-09-21T01:49:07","guid":{"rendered":"https:\/\/www.moonmile.net\/blog\/?p=11571"},"modified":"2025-09-21T21:27:29","modified_gmt":"2025-09-21T12:27:29","slug":"c-%e3%81%ae%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%bc%e3%83%aa%e3%83%bc%e3%82%af%e7%8f%be%e8%b1%a1%e3%81%ae%e7%b4%b9%e4%bb%8b%ef%bc%88%e6%a4%9c%e8%a8%bc%e3%82%b3%e3%83%bc%e3%83%89%e4%bb%98%e3%81%8d","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/11571","title":{"rendered":"C# \u306e\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u73fe\u8c61\u306e\u7d39\u4ecb\uff08\u691c\u8a3c\u30b3\u30fc\u30c9\u4ed8\u304d\uff09"},"content":{"rendered":"\n<p>\u4e00\u9023\u306e C# dis \u304b\u3089\u98db\u3073\u706b\uff1f\u3057\u305f\u306e\u304b\u3001Huomoto san \u306e\u30dd\u30b9\u30c8\u306b\u3001\u3055\u307e\u3056\u307e\u306a\u300cGC \u3060\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3057\u306a\u3044\u300d\u610f\u898b\u304c\u98db\u3073\u4ea4\u3063\u3066\u3044\u307e\u3059\u304c\u3001C# \u3067\u3082\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3057\u307e\u3059\u3088\u3001\u3063\u3066\u306e\u3092\u8a3c\u5de6\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"embed-twitter\"><blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"ja\" dir=\"ltr\">C#\u306f\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u306b\u8981\u6ce8\u610f\u306a\u3093\u3060\u3088\u306d\u3002\u5927\u91cf\u306e\u6587\u5b57\u5217\u3092\u8907\u96d1\u306b\u51e6\u7406\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\uff08\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u6539\u5909\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\uff09\u3092\u66f8\u3044\u305f\u3093\u3060\u3051\u3069\u3001\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u3067OS\u3054\u3068\u843d\u3061\u305f\u3002\u5bfe\u7b56\u306f\u51fa\u6765\u305f\u3051\u3069\u3001\u4e8b\u6545\u3067\u51e6\u7406\u3055\u308c\u3066\u73fe\u5834\u306e\u8a55\u4fa1\u304c\u4e0b\u304c\u3063\u305f\u3002.NET\u306f\u307f\u3093\u306a\u540c\u3058\u5371\u967a\u3042\u308b\u3002Java\u3082\u305d\u3046\u3002GC\u4ed8\u304d\u306e\u306f\u30af\u30fc\u30eb\u3067\u306f\u306a\u3044\u3002<\/p>&mdash; Huoumoto (@huoumoto) <a href=\"https:\/\/twitter.com\/huoumoto\/status\/1969262522218717274?ref_src=twsrc%5Etfw\">September 20, 2025<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/div>\n<\/div><\/figure>\n\n\n\n<p>\u53c2\u7167\u30ab\u30a6\u30f3\u30bf\u3067\u5236\u5fa1\u3057\u3066\u3044\u308b\u304b\u3089\u3001GC\uff08\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\uff09\u304c\u8d77\u3053\u3063\u3066\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u306f\u306a\u3044\u3001\u3068\u601d\u3044\u8fbc\u3093\u3067\u3044\u308b\u3068\u3001\u5b9f\u306f GC \u306e\u3042\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u8a00\u8a9e\uff08Java \u3082\u540c\u3058\uff09\u3067\u300c\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u95a2\u4fc2\u306e\u5bfe\u7b56\u3092\u898b\u843d\u3068\u3059\u300d\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u5371\u967a\u3067\u3059\u3002\u666e\u6bb5\u4f7f\u3063\u3066\u3044\u308b\u3068\u304d\u306b\u306f\u3001\u3042\u307e\u308a\u6c17\u306b\u3057\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u5236\u5fa1\u3068\u304b\u5897\u6e1b\u306e\u3042\u308b\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u6271\u3046\u3068\u304d\u306b\u3001\u6c17\u306b\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002\u306a\u3093\u304b\u3001\u30a4\u30a4\u611f\u3058\u306b\u30b3\u30fc\u30c9\u3092\u9ad8\u901f\u5316\u3057\u305f\u3068\u601d\u3063\u305f\u3089\u3001\u305d\u306e\u5148\u3067\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u3092\u8d77\u3053\u3059\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br>\u5f53\u7136\u3001\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u81ea\u4f53\u304c\u300c\u8a2d\u8a08\u30df\u30b9\u300d\u3068\u3044\u3046\u306e\u306f\u540c\u610f\u306f\u3057\u307e\u3059\u304c\u3001\u30ea\u30fc\u30af\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u3092\u5426\u5b9a\u3057\u307e\u3046\u3068\u3001\u8a2d\u8a08\u8003\u616e\u306e\u7bc4\u56f2\u5916\u306b\u306a\u3063\u3066\u3057\u307e\u3063\u3066\u3001\u8a2d\u8a08\u30df\u30b9\u306b\u3059\u3089\u6c17\u3065\u304b\u306a\u3044\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u5f8c\u77e5\u6075\u7684\u306b\u8a2d\u8a08\u30df\u30b9\u3084\u8003\u616e\u4e0d\u8db3\u3092\u306a\u3058\u308b\u3088\u308a\u3082\u3001\u5883\u754c\u5024\u306b\u8fd1\u3044\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5f97\u308b\u3088\u3046\u306a\u3068\u304d\u306f\u8ee2\u3070\u306c\u5148\u306e\u6756\u3001\u77f3\u6a4b\u3092\u53e9\u3044\u3066\u6e21\u308b\u6c17\u5206\u3067\u3044\u304d\u307e\u3057\u3087\u3046\u3002\u3082\u3061\u308d\u3093\u3001\u666e\u901a\u306e Web \u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u5834\u5408\u306b\u306f\u3042\u307e\u308a\u5fc5\u8981\u306a\u3044\u306e\u3067\u3059\u304c\uff08\u5b9f\u969b\u306b\u306f\u3001Web \u30b5\u30fc\u30d0\u30fc\u306e\u3068\u304d\u306f\u5fc5\u8981\u306a\u3093\u3060\u3051\u3069\u3001\u305d\u308c\u306f\u5225\u306e\u6a5f\u4f1a\u306b\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u751f\u6210\u6642<\/h2>\n\n\n\n<p>\u691c\u8a3c\u30b3\u30fc\u30c9\u3092\u4e00\u304b\u3089\u4f5c\u308b\u306e\u306f\u9762\u5012\u306a\u306e\u3067\u3001\u30d9\u30fc\u30b9\u306f Copilot \u306b\u4f5c\u3063\u3066\u8cb0\u3044\u307e\u3059\u3002Copilot \u306e\u51fa\u3057\u305f\u751f\u30b3\u30fc\u30c9\u306f\u3001\u305d\u306e\u307e\u307e\u3067\u306f\u99c4\u76ee\u306a\u306e\u3067\u3001\u591a\u5c11\u624b\u3092\u52a0\u3048\u3066\u3044\u307e\u3059\u304c\u3001\u4e00\u5fdc\u5408\u683c\u70b9\u306f\u4e0a\u3052\u3089\u308c\u308b\u3068\u601d\u3044\u307e\u3059\uff08\u8003\u3048\u3066\u3044\u308b\u304b\u3082\u3001\u3068\u3044\u3046\u610f\u5473\u3067\uff09<\/p>\n\n\n\n<p>\u30d7\u30ed\u30f3\u30d7\u30c8\u306f\u3001\u3053\u3093\u306a\u611f\u3058\u3002\u624b\u5143\u306e\u9069\u5f53\u306a\u751f\u6210AI\u30c1\u30e3\u30c3\u30c8\u3067\u3082\u53ef\u80fd\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">C# \u3067\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u3092\u3059\u308b\u3001\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u4f5c\u3063\u3066\u3002\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066 create object \u3092\u5927\u91cf\u306b\u3059\u308b\u3068\u53c2\u7167\u3055\u308c\u308b\u30dd\u30a4\u30f3\u30bf\u304c\u306a\u304f\u3066\u3082\u3001\u624b\u52d5\u306e GC \u3092\u52d5\u304b\u3055\u306a\u3044\u3068\u30e1\u30e2\u30ea\u304c\u89e3\u653e\u3055\u308c\u306a\u3044\u72b6\u614b\u306b\u306a\u308b<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>using System;\nusing System.Collections.Generic;\nusing System.Reflection;\n\nnamespace MemoryLeakDemo\n{\n    class LeakyClass \n    {\n        public byte[] Data = new byte[1024 * 1024]; \/\/ 1MB\n        public LeakyClass()\n        {\n            \/\/ \u30e9\u30f3\u30c0\u30e0\u306b\u30c7\u30fc\u30bf\u3092\u521d\u671f\u5316\n            new Random().NextBytes(Data);\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            List&lt;object&gt; leakyList = new List&lt;object&gt;();\n\n            for (int i = 0; i &lt; 5000; i++)\n            {\n#if false\n                \/\/ \u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\n                Type type = typeof(LeakyClass);\n                ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);\n                object instance = ctor.Invoke(null);\n#else\n                \/\/ \u901a\u5e38\u306e\u65b9\u6cd5\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\n                LeakyClass instance = new LeakyClass();\n#endif\n                \/\/ Finalizer \u3092\u6291\u5236\uff08GC \u306e\u5bfe\u8c61\u306b\u306a\u308a\u306b\u304f\u304f\u306a\u308b\uff09\n                \/\/ GC.SuppressFinalize(instance);\n\n                \/\/ \u660e\u793a\u7684\u306b\u53c2\u7167\u3092\u4fdd\u6301\uff08GC \u304c\u56de\u53ce\u3067\u304d\u306a\u3044\uff09\n                leakyList.Add(instance);\n\n                Console.WriteLine($&quot;Created instance {i}&quot;);\n            }\n\n            Console.WriteLine(&quot;\u5927\u91cf\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3057\u307e\u3057\u305f\u3002GC \u3092\u5f37\u5236\u3057\u306a\u3044\u9650\u308a\u30e1\u30e2\u30ea\u306f\u89e3\u653e\u3055\u308c\u307e\u305b\u3093\u3002&quot;);\n            Console.ReadLine();\n\n            \/\/ \u89e3\u653e\u3059\u308b\u7b48\n            leakyList.Clear();\n            Console.WriteLine(&quot;leakyList \u306e\u89e3\u653e&quot;);\n            Console.ReadLine();\n\n            \/\/ \u624b\u52d5\u3067 GC \u3092\u52d5\u304b\u3059\u3053\u3068\u3067\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u53ef\u80fd\n            GC.Collect();\n            \/\/ GC.WaitForPendingFinalizers();\n\n            Console.WriteLine(&quot;GC \u3092\u5f37\u5236\u5b9f\u884c\u3057\u307e\u3057\u305f\u3002&quot;);\n            Console.ReadLine();\n        }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>type.GetConstructor \u306e\u3088\u3046\u306b\u3001DI \u3092\u4f7f\u3046\u3068\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u304c\u767a\u751f\u3057\u3084\u3059\u3044\u3067\u3059\u3002GC \u304c\u8ffd\u8de1\u3057\u306b\u304f\u304f\u306a\u308b\u3093\u3067\u3059\u306d\u3002\u5b9f\u306f\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3060\u3068 new LeakyClass() \u3067\u3082\u30ea\u30fc\u30af\u3059\u308b\u306e\u3067\u3001\u691c\u8a3c\u30b3\u30fc\u30c9\u3068\u3057\u3066\u306f\u3044\u307e\u3044\u3061\u306a\u3093\u3067\u3059\u304c\u3001\u307e\u3042\u3001\u30ea\u30fc\u30af\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3068\u3044\u3046\u3053\u3068\u3067\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>leakyList.Add(instance) \u3067\u3001\u53c2\u7167\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u304c\u3001<\/li>\n\n\n\n<li>leakyList.Clear(); \u3067\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3066\u3044\u308b\u3068\u601d\u3063\u305f\u304c\u3001\u89e3\u653e\u306f\u3055\u308c\u306a\u3044<\/li>\n\n\n\n<li>\u660e\u793a\u7684\u306b GC.Collect(); \u304c\u5fc5\u8981<\/li>\n<\/ol>\n\n\n\n<p>\u304a\u305d\u3089\u304f\u30b9\u30b3\u30fc\u30d7\u306e\u95a2\u4fc2\u3060\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u30013 \u306e\u6642\u70b9\u3067\u3001GC.Collect() \u3092\u5b9f\u884c\u3057\u3066\u3082\u89e3\u653e\u306f\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"874\" height=\"609\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-5.png\" alt=\"\" class=\"wp-image-11572\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-5.png 874w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-5-300x209.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-5-768x535.png 768w\" sizes=\"auto, (max-width: 874px) 100vw, 874px\" \/><\/a><\/figure>\n\n\n\n<p>\u3053\u308c\u306f\u3001.NET \u306e\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u89e3\u653e\u304c3\u7a2e\u985e\u3042\u3063\u3066\u3001\u4e16\u4ee3\u3054\u3068\u306b\u89e3\u653e\u3055\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u9055\u3046\u304b\u3089\u3067\u3059\u3002\u5927\u91cf\u306b\u53d6\u5f97\u3057\u305f new LeakyClass \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306f\u3001leakyList.Clear() \u3057\u305f\u6642\u70b9\u3067\u89e3\u653e\u3055\u308c\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u3053\u306e\u77ac\u9593\u3067\u306f\u89e3\u653e\u3055\u308c\u307e\u305b\u3093\u3002\u30e1\u30e2\u30ea\u306e\u72b6\u614b\u3084\u751f\u5b58\u671f\u9593\uff08lifetime\uff09\u306b\u3088\u3063\u3066\u306f\u3001GC.Collect \u306e\u547c\u3073\u51fa\u3057\u5f8c\u3082\u6b8b\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002.NET \u306e GC \u306e\u5834\u5408\u3001GC \u30e1\u30e2\u30ea\u304c\u81ea\u52d5\u62e1\u5f35\u3055\u308c\u308b\u305f\u3081\u306b\u3001\u7269\u7406\u30e1\u30e2\u30ea\u3092\u5727\u8feb\u3057\u3066\u3057\u307e\u3046\u30ea\u30b9\u30af\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u306f\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u30671\u3064\u3057\u304b\u52d5\u304b\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3059\u304c\u3001Web \u30b5\u30fc\u30d0\u30fc\u3067\u306e\u5185\u90e8\u51e6\u7406\u3084\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30b5\u30fc\u30d0\u30fc\u306e\u3088\u3046\u306a\u6271\u3044\u3092\u3057\u3066\u3044\u308b\u3068\u304d\u306f\u3001\u3053\u306e\u3088\u3046\u306a\u4e0d\u610f\u306a\u30e1\u30e2\u30ea\u53d6\u5f97\u306f\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u60aa\u5f71\u97ff\u3092\u4e0e\u3048\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u89e3\u6c7a\u6848\u3068\u3057\u3066\u306f\u3001LeakyClass \u5c02\u7528\u306e\u30a2\u30ed\u30b1\u30fc\u30bf\u30fc\u3092\u4f5c\u308a\u307e\u3059\u30025000\u4ef6\u3050\u3089\u3044\u3060\u3068 IDisposable \u3092\u5b9f\u88c5\u3057\u3066\u5185\u90e8\u30e1\u30e2\u30ea\u3092\u5916\u51fa\u3057\u306b\u3059\u308b\u3068\u304b\u3001\u30ea\u30fc\u30af\u306e\u91cf\u3092\u6e1b\u3089\u3059\u65b9\u6cd5\u3082\u6d3b\u7528\u3067\u304d\u307e\u3059\u3002\u30b2\u30fc\u30e0\u30b5\u30fc\u30d0\u30fc\u3060\u3068 100\u4e07\u4ef6\u3068\u304b\u304c\u591a\u3044\u3067\u3057\u3087\u3046\u304b\u3089\u3001\u3082\u3046\u5c11\u3057\u5225\u306a\u5bfe\u51e6\u3092\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3067\u3057\u3087\u3046\uff08\u30b2\u30fc\u30e0\u306e\u65b9\u306f\u77e5\u3089\u306a\u3044\u306e\u3067\u3001\u8abf\u3079\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u76f8\u4e92\u53c2\u7167\u306b\u3088\u308b\u30e1\u30e2\u30ea\u30ea\u30fc\u30af<\/h2>\n\n\n\n<p>\u3082\u3046\u3072\u3068\u3064\u5178\u578b\u7684\u306a\u30ea\u30fc\u30af\u306e\u30d1\u30bf\u30fc\u30f3\u3092\u7d39\u4ecb\u3057\u307e\u3057\u3087\u3046\u3002\u30ea\u30b9\u30c8\u69cb\u9020\u306e\u4e2d\u3067\u3001\u5404\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u76f8\u4e92\u53c2\u7167\u3084\u5faa\u74b0\u53c2\u7167\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u3067\u3059\u3002\u3053\u308c\u306f\u53c2\u7167\u3057\u3066\u3044\u308b\u306e\u3060\u304b\u3089\u3001\u53c2\u7167\u30ab\u30a6\u30f3\u30c8\u3067 GC \u306e\u5bfe\u8c61\u306b\u306a\u3089\u306a\u3044\u306e\u3067\u5408\u3063\u3066\u306f\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u7fa4\u3092\u7ba1\u7406\u3057\u3066\u3044\u308b\u30ea\u30b9\u30c8\u3084\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u3092\u89e3\u653e\u3057\u3066\u3082\u3001\u305d\u306e\u5148\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u89e3\u653e\u3055\u308c\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br>\u3053\u308c\u306f\u3001GC \u306e\u4ed5\u69d8\u3067\u3001\u89e3\u653e\u306e\u6df1\u5ea6\u306f\u305d\u3053\u307e\u3067\u8ffd\u308f\u306a\u3044\u3068\u3044\u3046\u8a2d\u8a08\u306b\u306a\u3063\u3066\u3044\u308b\u305f\u3081\u3067\u3059\u3002\u8ffd\u3048\u306a\u3044\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u3057\u3087\u3046\u304c\u3001GC \u306e\u305f\u3081\u306b\u305d\u3053\u307e\u3067\u5b9f\u884c\u6642\u306b\u6642\u9593\u3092\u639b\u3051\u308b\u306e\u306f\u99ac\u9e7f\u99ac\u9e7f\u3057\u3044\u306e\u3067\u3001\u5b9f\u88c5\u8005\u306e\u307b\u3046\u3067\u30e1\u30e2\u30ea\u3092\u7ba1\u7406\u3057\u3066\u306d\u3001\u3068\u3044\u3046\u300c\u610f\u56f3\u7684\u306a\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u300d\u3067\u3059\u3002<br>\u306a\u306e\u3067\u3001\u3053\u306e\u610f\u56f3\u7684\u306a GC \u306e\u8a2d\u8a08\u3092\u77e5\u3089\u306a\u3044\u3068\u3001\u76f8\u4e92\u53c2\u7167\u3092\u3055\u305b\u3066\u30ea\u30fc\u30af\u3057\u3066\u3057\u307e\u3046\u3093\u3067\u3059\u306d\u3002\u3064\u307e\u308a\u306f\u300c\u4ed5\u69d8\u3067\u3059\u300d\u3068\u3044\u3046\u3084\u3064\u3067\u3059\u3002\u3053\u306e GC \u8a2d\u8a08\u306f .NET \u7279\u6709\u306a\u306e\u304b\u3001JVM \u306e GC \u306b\u3082\u4f7f\u308f\u308c\u3066\u3044\u308b\u304b\u79c1\u306f\u77e5\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u76f8\u4e92\u53c2\u7167\u3092\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u304a\u304a\u3082\u3068\u306e\u53c2\u7167\u30dd\u30a4\u30f3\u30bf\u3092\u6d88\u3057\u3066\u3082\u76f8\u4e92\u53c2\u7167\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u6b8b\u3063\u3066\u3057\u307e\u3046\u4f8b\u3092\u793a\u3057\u3066\u3002<\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>using System;\nusing System.Collections.Generic;\nusing static System.Runtime.InteropServices.JavaScript.JSType;\n\nnamespace CyclicReferenceLeak\n{\n    class Node\n    {\n        public Node Partner;\n        public byte[] Payload = new byte[1024 * 1024]; \/\/ 1MB\n\n        public Node()\n        {\n            \/\/ \u30e9\u30f3\u30c0\u30e0\u306b\u30c7\u30fc\u30bf\u3092\u521d\u671f\u5316\n            new Random().NextBytes(Payload);\n        }\n\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            List&lt;Node&gt; rootList = new List&lt;Node&gt;();\n\n            for (int i = 0; i &lt; 5000; i++)\n            {\n                Node a = new Node();\n                Node b = new Node();\n\n                \/\/ \u76f8\u4e92\u53c2\u7167\u3092\u69cb\u7bc9\n                a.Partner = b;\n                b.Partner = a;\n\n                \/\/ rootList \u306b\u8ffd\u52a0\uff08GC \u306e\u5bfe\u8c61\u5916\uff09\n                rootList.Add(a);\n            }\n\n            Console.WriteLine(&quot;\u76f8\u4e92\u53c2\u7167\u3057\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 1000 \u7d44\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002Enter \u3067 rootList \u3092\u30af\u30ea\u30a2\u3057\u307e\u3059\u3002&quot;);\n            Console.ReadLine();\n\n            \/\/ rootList \u3092\u30af\u30ea\u30a2 \u2192 \u304a\u304a\u3082\u3068\u306e\u53c2\u7167\u304c\u6d88\u3048\u308b\n            rootList.Clear();\n            Console.WriteLine(&quot;rootList \u3092\u30af\u30ea\u30a2\u3057\u307e\u3057\u305f\u3002GC \u3092\u5f37\u5236\u3057\u306a\u3044\u9650\u308a\u3001\u76f8\u4e92\u53c2\u7167\u306e\u305f\u3081\u30e1\u30e2\u30ea\u306f\u89e3\u653e\u3055\u308c\u307e\u305b\u3093\u3002&quot;);\n            Console.ReadLine();\n\n            \/\/ GC \u3092\u5f37\u5236\u5b9f\u884c\n            GC.Collect();\n            GC.WaitForPendingFinalizers();\n\n            Console.WriteLine(&quot;GC \u3092\u5f37\u5236\u5b9f\u884c\u3057\u307e\u3057\u305f\u3002&quot;);\n            Console.ReadLine();\n        }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>\u3053\u308c\u3082 Copilot \u306b\u4f5c\u3063\u3066\u8cb0\u3063\u305f\u3082\u306e\u3067\uff08\u3064\u307e\u308a\u306f\u3001Copilot \u306e codex \u306e\u30e2\u30c7\u30eb\u306b\u306f GC \u306e\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u95a2\u4fc2\u306e\u60c5\u5831\u304c\u5165\u3063\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3088\uff09\u3001Node \u306e a \u3068 b \u304c\u76f8\u4e92\u306b\u53c2\u7167\u3057\u3066\u3044\u307e\u3059\u3002\u4fdd\u6301\u30ea\u30b9\u30c8\u3068\u3057\u3066 rootList.Add \u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001rootList.Clear() \u306e\u77ac\u9593\u306b\u306f\u30e1\u30e2\u30ea\u306f\u89e3\u653e\u3055\u308c\u307e\u305b\u3093\u3002\u3053\u308c\u3082 GC \u306e\u4ed5\u69d8\u3067\u3059\u3002\u305d\u306e\u3042\u3068\u306b\u3001GC.Collect() \u3092\u5b9f\u884c\u3057\u307e\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001\u3053\u306e PC \u3067\u306f\u30e1\u30e2\u30ea\u306f\u89e3\u653e\u3055\u308c\u307e\u305b\u3093\u3002<br>\u3053\u306e\u696d\u52d9 PC \u306f\u3001\u5148\u65e5\u30e1\u30e2\u30ea\u3092\u8c4a\u5bcc\u306b\u3057\u305f\u3070\u304b\u308a\u306a\u306e\u3067\u300110GB \u7a0b\u5ea6\u3067\u306f\u3073\u304f\u3068\u3082\u3057\u306a\u3044\u3093\u3067\u3059\u3088\u306d\u3002\u3082\u3046\u3061\u3087\u3063\u3068\u30c1\u30fc\u30d7\u306a\u30e1\u30e2\u30ea\uff088GB\u3068\u304b16GB\u3042\u305f\u308a\uff09\u3067\u8a66\u3057\u3066\u307f\u308b\u3068\u3001OS \u3054\u3068\u30ed\u30c3\u30af\u3055\u308c\u308b\u304b\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u56fa\u307e\u308b\u306e\u304c\u5224\u5225\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002Windows \u4e0a\u3067\u3084\u308b\u3068\u3001Swap \u30d5\u30a1\u30a4\u30eb\u3092\u98df\u3044\u5c3d\u304f\u3057\u3066 C \u30c9\u30e9\u30a4\u30d6\u306e SSD \u304c\u3044\u3063\u3071\u3044\u3044\u3063\u3071\u3044\u306b\u306a\u308b\u306e\u3067\u3001\u3084\u3081\u3066\u304a\u3044\u305f\u307b\u3046\u304c\u826f\u3044\u3067\u3059\u3002OS \u304c\u843d\u3061\u308b\u73fe\u8c61\u306f\u3001\u30e1\u30e2\u30ea\u30fc\u30ea\u30fc\u30af\u3067\u843d\u3061\u308b\u306e\u3067\u306f\u306a\u304f\u3001swap \u30d5\u30a1\u30a4\u30eb\u306e\u98df\u3044\u5c3d\u304f\u3057\u3067 OS \u304c\u5229\u7528\u3059\u308b\u30c6\u30f3\u30dd\u30e9\u30ea\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u308c\u306a\u304f\u306a\u3063\u305f\u3068\u304d\u3067\u3059\u3002\u30d6\u30eb\u30fc\u30b9\u30af\u30ea\u30fc\u30f3\u306b\u306a\u308a\u307e\u3059\u306d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"886\" height=\"578\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-6.png\" alt=\"\" class=\"wp-image-11573\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-6.png 886w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-6-300x196.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-6-768x501.png 768w\" sizes=\"auto, (max-width: 886px) 100vw, 886px\" \/><\/a><\/figure>\n\n\n\n<p>\u6b63\u3057\u304f\u691c\u8a3c\u3057\u305f\u3044\u5834\u5408\u306f\u3001Hyper-V \u3067 8GB \u7a0b\u5ea6\u306e Windows 11 \u3092\u4f5c\u6210\u3057\u3066\u3001\u305d\u306e\u4e2d\u3067\u5b9f\u9a13\u3059\u308b\u3068\u3088\u3044\u3067\u3059\u3002\u30e1\u30e2\u30ea\u3092\u5236\u9650\u3055\u305b\u308b\u3068\u3001swap \u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u69d8\u5b50\u304c\u308f\u304b\u308a\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u30d7\u30ed\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u308b\u305f\u3081\u306b Visual Studio \u3092\u5165\u308c\u306a\u3044\u3068\u3051\u306a\u3044\u306e\u304c\u7d50\u69cb\u624b\u9593\u3067\u3059\u306d\u3002\u4ed5\u4e8b\u3067\u3084\u308b\u3068 1,2 \u65e5\u304c\u304b\u308a\u3068\u3044\u3046\u3068\u3053\u308d\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u66f8<\/h2>\n\n\n\n<p>\u300cGarbage Collection\u300d\u3068\u3044\u3046\u6d0b\u66f8\u304c\u3042\u3063\u3066\u3001\u3053\u308c\u306f\u307b\u307c\u552f\u4e00\u306e GC \u306e\u89e3\u8aac\u66f8\u3067\u3059\u3002\u4ed6\u306b\u3082 \u300c\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 .NET Framework\u300d\u306e\u672c\u306b\u3082 GC \u306e\u7ae0\u306f\u3042\u308b\u306e\u3067\u3059\u304c\u3001\u3042\u307e\u308a\u89e3\u8aac\u306f\u306a\u3044\uff08GC.Collect \u3042\u305f\u308a\u306e\u3056\u3063\u3068\u3057\u305f\u89e3\u8aac\uff09\u306e\u3067\u3001\u307e\u3042\u3001\u3053\u308c\u3092\u8cb7\u3046\u3057\u304b\u306a\u3044\u3067\u3059\u3088\u306d\uff08\u3068\u8a00\u3063\u3066\u307f\u308b\u30c6\u30b9\u30c8\uff09\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7-1024x768.png\" alt=\"\" class=\"wp-image-11574\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7-1024x768.png 1024w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7-300x225.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7-768x576.png 768w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-7.png 1324w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"745\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-8.png\" alt=\"\" class=\"wp-image-11575\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-8.png 993w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-8-300x225.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/09\/image-8-768x576.png 768w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/a><\/figure>\n\n\n\n<p>\u305f\u3060\u3001\u3053\u308c\u3092\u8cb7\u3063\u305f\u306e\u306f15\u5e74\u7a0b\u524d\u306a\u306e\u3067\u3001\u3044\u307e\u3060\u3068\u3044\u304f\u3064\u304b\u51fa\u3066\u3044\u307e\u3059\u306d\u3002GC \u306e\u4e16\u4ee3\u3068\u304b\u3001\u30de\u30fc\u30af\uff06\u30b9\u30a4\u30fc\u30d7\u65b9\u5f0f\u306e\u8a73\u3057\u3044\u89e3\u8aac\u3068\u304b\u56f3\u3064\u304d\u3067\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f59\u8ac7\u3067\u3059\u304c\u3001java \u306e\u5834\u5408\u306f -vm \u3067\u30e1\u30e2\u30ea\u30b5\u30a4\u30ba\u3092\u5236\u5fa1\u3067\u304d\u308b\u306e\u3067\u3001.NET \u74b0\u5883\u306e\u3088\u3046\u306a swap \u3092\u98df\u3044\u5c3d\u304f\u3059\u3053\u3068\u304c\u3042\u308a\u307e\u305b\u3093\u3002\u305f\u3060\u3057\u3001java \u306e\u5834\u5408\u306f -vm \u306e\u30e1\u30e2\u30ea\u3092\u55b0\u3044\u4ed8\u304f\u3057\u3066\u3057\u307e\u3063\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u4e0d\u610f\u306b\u6b62\u307e\u308a\u307e\u3059\u3002\u3053\u308c\u3001\u3069\u3061\u3089\u3092\u512a\u5148\u3059\u308b\u304b\u306b\u3082\u3088\u308b\u306e\u3067\u3059\u304c\u3001\u904b\u7528\u6642\u306b\u30a2\u30d7\u30ea\u304c\u843d\u3061\u306a\u3044\u3053\u3068\u3092\u512a\u5148\uff1d.NET\u306e GC \u306b\u3059\u308b\u304b\u3001\u904b\u7528\u6642\u306b OS \u304c\u843d\u3061\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u304b\uff1d Java \u306e GC \u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u4eca\u3060\u3068\u30e1\u30e2\u30ea\u304c\u6f64\u6ca2\u306b\u3042\u308b\u306e\u3067\u3001Java \u306e -vm \u3092\u5927\u304d\u76ee\u306b\u53d6\u3063\u3066\u304a\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u30b5\u30fc\u30d0\u30fc\u904b\u7528\u306e\u3068\u304d\u306f -vm \u3092\u5927\u304d\u304f\u53d6\u308a\u904e\u304e\u308b\u3068\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\uff08\u4ed6\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3082 java \u3060\u3063\u305f\u308a\u3059\u308b\uff09\u3092\u5727\u8feb\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3001.NET \u65b9\u5f0f\u304c\u826f\u304b\u3063\u305f\u6642\u671f\u3082\u3042\u308b\u306e\u3067\u3059\u3002\u3053\u308c\u306f\u3069\u3061\u3089\u306e\u65b9\u5f0f\u3082\u53d6\u308c\u308b\u3068\u3044\u3044\u3093\u3067\u3059\u304c\u3001\u4eca\u3060\u3068\u3067\u304d\u308b\u306e\u304b\u306a\u300210\u5e74\u7a0b\u524d .NET \u306e\u307b\u3046\u3067\u8abf\u3079\u305f\u3068\u304d\u306b\u306f\u3001-vm \u65b9\u5f0f\u304c\u4f7f\u3048\u306a\u3044\u306e\u3067\u3001\u5148\u306e\u7d39\u4ecb\u3057\u305f\u3088\u3046\u306a\u72ec\u81ea\u30a2\u30ed\u30b1\u30fc\u30c8\u65b9\u5f0f\u3067\u5bfe\u5fdc\u3059\u308b\u3053\u3068\u306b\u3057\u305f\u306e\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u9023\u306e C# dis \u304b\u3089\u98db\u3073\u706b\uff1f\u3057\u305f\u306e\u304b\u3001Huomoto san \u306e\u30dd\u30b9\u30c8\u306b\u3001\u3055\u307e\u3056\u307e\u306a\u300cGC \u3060\u3068\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3057\u306a\u3044\u300d\u610f\u898b\u304c\u98db\u3073\u4ea4\u3063\u3066\u3044\u307e\u3059\u304c\u3001C# \u3067\u3082\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3057\u307e\u3059\u3088\u3001\u3063\u3066\u306e\u3092\u8a3c\u5de6\u3057\u3066\u304a\u304d\u307e\u3059\u3002 \u53c2\u7167\u30ab\u30a6\u30f3\u30bf &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/11571\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-11571","post","type-post","status-publish","format-standard","hentry","category-dev"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=11571"}],"version-history":[{"count":5,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11571\/revisions"}],"predecessor-version":[{"id":11582,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11571\/revisions\/11582"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=11571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=11571"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=11571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}