{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/",
  "atlas": "https://www.pystone.net/?node=uwa-answer-new-struct-in-update#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update.md",
  "context": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:uwa-answer-new-struct-in-update",
  "slug": "uwa-answer-new-struct-in-update",
  "title": "Update中new struct对象",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "石心.Yong"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Update中new struct对象",
  "contentRole": "source",
  "sourceType": "source",
  "sourceMetadata": {
    "author": "石心.Yong"
  },
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "external_source",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "28cbd181559be077b4b6d7d714e6c7f3696a3fe6352c63e6af216cfdde0df7ae",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "Update中new struct对象",
      "anchor": "update中new-struct对象",
      "citation": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/#update%E4%B8%ADnew-struct%E5%AF%B9%E8%B1%A1"
    },
    {
      "depth": 2,
      "text": "问题",
      "anchor": "问题",
      "citation": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/#%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "我的回答",
      "anchor": "我的回答",
      "citation": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/#%E6%88%91%E7%9A%84%E5%9B%9E%E7%AD%94"
    },
    {
      "depth": 2,
      "text": "来源与关系",
      "anchor": "来源与关系",
      "citation": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/#%E6%9D%A5%E6%BA%90%E4%B8%8E%E5%85%B3%E7%B3%BB"
    }
  ],
  "claims": [],
  "outgoing": [
    {
      "id": "note:my-uwa-answers",
      "title": "我的 UWA 问答",
      "url": "https://www.pystone.net/notes/my-uwa-answers/",
      "atlas": "https://www.pystone.net/?node=my-uwa-answers#knowledge-atlas",
      "label": "我的 UWA 问答",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "上位集合：我的 UWA 问答",
      "citation": "https://www.pystone.net/notes/uwa-answer-new-struct-in-update/#%E6%9D%A5%E6%BA%90%E4%B8%8E%E5%85%B3%E7%B3%BB"
    }
  ],
  "incoming": [
    {
      "id": "note:my-uwa-answers",
      "title": "我的 UWA 问答",
      "url": "https://www.pystone.net/notes/my-uwa-answers/",
      "atlas": "https://www.pystone.net/?node=my-uwa-answers#knowledge-atlas",
      "label": "我的 UWA 问答",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "2021-09-02：Update中new struct对象（内存管理）",
      "citation": "https://www.pystone.net/notes/my-uwa-answers/#2021"
    },
    {
      "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": "我的UWA问答中的“2021-09-02 - Update中new struct对象”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#%E6%88%91%E7%9A%84uwa%E9%97%AE%E7%AD%94"
    }
  ],
  "contentMarkdown": "# Update中new struct对象\n\n> [!note] 我的 UWA 问答\n> 本文收录我在 UWA 问答发表的公开回答。问题内容归原提问者；回答保留当时语境，技术结论可能受 Unity 版本、平台和项目条件限制。\n\n## 问题\n\nstruct中包含string字段，如果在Update中new，会不会有gc问题，考虑是否需要用缓存池来解决，目前我测试了一下将struct改成class是肯定有，如果是struct类型，好像没找到\n\n- 提问者：aladdin\n- 提问时间：2021-09-02 18:17:35\n\n## 我的回答\n\n对class进行new是会分配堆内存，但是这个堆内存不一定是string字段造成的。class的实例本身就会占用堆内存，而他的字段会不会造成堆内存分配，取决于new的时候会不会对类型进行实例化。实例化时候给string赋值，如果是直接确定的字符串内容的话，编译器会自动进行缓存优化，只会在第一帧创建字符串分配内存。\n\n至于struct，结构体作为值类型本身不会占用堆内存。对struct进行new会不会分配堆内存，取决于new的时候，他的字段会不会实例化，会不会占用堆内存。如果struct没有写构造函数的话，即使里面有引用类型，在new struct的时候也不会实例化，不会分配堆内存。如果写了构造函数，就要看构造函数里面是否分配堆内存了。string的情况与上文提到的一致。\n\n- 回答时间：2021-09-02 21:21:12\n- 最后更新：2021-09-02 21:21:12\n- 采纳状态：未采纳\n\n## 来源与关系\n\n- 上位集合：[[10-计算机、信息技术与工程/05-游戏图形与运行时/我的UWA问答/我的UWA问答|我的 UWA 问答]]\n- UWA 标签：内存管理\n- 原始问题：[Update中new struct对象](https://answer.uwa4d.com/question/6130343f8f8c8342418ff792)\n- 源页最后更新：2021-09-05 22:32:36\n"
}
