{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/unity-destroy/",
  "atlas": "https://www.pystone.net/?node=unity-destroy#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/unity-destroy.md",
  "context": "https://www.pystone.net/notes/unity-destroy.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:unity-destroy",
  "slug": "unity-destroy",
  "title": "Destroy",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Destroy",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "81489f2ba80faa7e9d9bc04c69ddce7973a3cb0b3bb026559f666615ee381e6e",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "Destroy",
      "anchor": "destroy",
      "citation": "https://www.pystone.net/notes/unity-destroy/#destroy"
    },
    {
      "depth": 2,
      "text": "概述",
      "anchor": "概述",
      "citation": "https://www.pystone.net/notes/unity-destroy/#%E6%A6%82%E8%BF%B0"
    },
    {
      "depth": 2,
      "text": "注意点",
      "anchor": "注意点",
      "citation": "https://www.pystone.net/notes/unity-destroy/#%E6%B3%A8%E6%84%8F%E7%82%B9"
    },
    {
      "depth": 2,
      "text": "测试",
      "anchor": "测试",
      "citation": "https://www.pystone.net/notes/unity-destroy/#%E6%B5%8B%E8%AF%95"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "Unity中的“Destroy”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#unity"
    }
  ],
  "contentMarkdown": "# Destroy\n\n> 创建时间：2020/6/19 17:02\n\nRef: <https://www.cnblogs.com/timeObjserver/p/7575035.html>\n\n## 概述\n\nUNITY Destroy()和DestroyImadiate()都不会立即释放对象内存\n\nDestroyImadiate是立即将物体从场景hierachy中移除，并标记为 “null”，注意 是带引号的null。这是UNITY内部的一个处理技巧。关于这个技巧有很争议。\n\nDestroy要等到帧末才会将物体从场景层级中移除并标记为”null”。\n\n不管如何，二者都只是UNITY引擎层面的标记与处理，但在.NET底层，对象的内存都没有释放，只有手动GC.Collect()或等待NET去GC时才会释放掉对象内存。\n如果该对象在其他地方还有引用的话，GC也无法将对象的内存释放。\n\n## 注意点\n\n（1）obj并不会立即销毁，而是需要等待下一个Update更新，所以还是可以被print出来；\n（2）DestroyImmediate立即对对像进行销毁，print出来是null；\n（3）Destroy销毁场景中的物体，但内存中还存在，当令它需要销毁时，只是给一个标识。而内存中它依然是存在的，只有当内存不够，或一段时间没有再次被引用时（或者更多合理的条件满足），机制才会将它销毁并释放内存；\n（4）这样做的目的就是为了避免频繁对内存的读写操作。回收器会定时清理一次内存中引用计数为0的对象，很可能你的要销毁的对象在其他地方还有引用而你自己不清楚，直接销毁可能导致其他地方空引用错误；\n（5）建议使用平常Destroy函数，而不是DestroyImmediate函数；\n\n## 测试\n\n测试代码如下：点ADD按钮不断创建对象，点DEL按钮清除所有对象，通过观察进程内存数值来察看对象内存是否释放。\n\n```csharp\nstring s1 = &quot;Hello &quot;;\nstring s2 = s1;\ns1 += &quot;World&quot;;\n\nSystem.Console.WriteLine(s2);\n//Output: Hello\n\n```\n"
}
