{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/memory-internals-deep-dive/",
  "atlas": "https://www.pystone.net/?node=memory-internals-deep-dive#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/memory-internals-deep-dive.md",
  "context": "https://www.pystone.net/notes/memory-internals-deep-dive.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:memory-internals-deep-dive",
  "slug": "memory-internals-deep-dive",
  "title": "【内存】内存底层深入",
  "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": "【内存】内存底层深入 创建时间：2021/7/2 12:59",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "d8cd3ff8ec1b3fc012d882060e3cdff9c299386a614001683692693972a3f5fb",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "【内存】内存底层深入",
      "anchor": "内存内存底层深入",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E5%86%85%E5%AD%98%E5%86%85%E5%AD%98%E5%BA%95%E5%B1%82%E6%B7%B1%E5%85%A5"
    },
    {
      "depth": 2,
      "text": "【内存】内存底层深入",
      "anchor": "内存内存底层深入-1",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E5%86%85%E5%AD%98%E5%86%85%E5%AD%98%E5%BA%95%E5%B1%82%E6%B7%B1%E5%85%A5-1"
    },
    {
      "depth": 2,
      "text": "基本知识",
      "anchor": "基本知识",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E5%9F%BA%E6%9C%AC%E7%9F%A5%E8%AF%86"
    },
    {
      "depth": 3,
      "text": "物理内存的访问",
      "anchor": "物理内存的访问",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E7%89%A9%E7%90%86%E5%86%85%E5%AD%98%E7%9A%84%E8%AE%BF%E9%97%AE"
    },
    {
      "depth": 3,
      "text": "台式设备和移动设备内存架构差异",
      "anchor": "台式设备和移动设备内存架构差异",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E5%8F%B0%E5%BC%8F%E8%AE%BE%E5%A4%87%E5%92%8C%E7%A7%BB%E5%8A%A8%E8%AE%BE%E5%A4%87%E5%86%85%E5%AD%98%E6%9E%B6%E6%9E%84%E5%B7%AE%E5%BC%82"
    },
    {
      "depth": 2,
      "text": "内存分页",
      "anchor": "内存分页",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#%E5%86%85%E5%AD%98%E5%88%86%E9%A1%B5"
    },
    {
      "depth": 2,
      "text": "Unity",
      "anchor": "unity",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#unity"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/memory-internals-deep-dive/#ref"
    }
  ],
  "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中的“【内存】内存底层深入”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#unity"
    }
  ],
  "contentMarkdown": "# 【内存】内存底层深入\n> 创建时间：2021/7/2 12:59\n\n## 【内存】内存底层深入\n\n  * 【内存】内存底层深入\n    * 基本知识\n      * 物理内存的访问\n      * 台式设备和移动设备内存架构差异\n    * 内存分页\n    * Unity\n    * Ref\n\n## 基本知识\n\n### 物理内存的访问\n\nCPU访问内存是一个慢速过程。\n访问过程：先访问Cache，Cache包含L1，L2，L3，也就是一级缓存，二级缓存和三级缓存，若在这些缓存里全没找到我们要的数据，再去访问内存，接着会把找到的数据存放到Cache中，完成一次操作。\n\n过多的Cache Miss就会导致大量的内存和Cache的IO交换，浪费大量时间。\n\n> Unity ECS方案: 可以将存储在内存中的不连续数据，变为连续的数据，从而降低Cache Miss的概率。\n\n### 台式设备和移动设备内存架构差异\n\n  * 移动设备 **没有独立显卡** 。\n\n  * 移动设备 **没有独立显存** ，内存和显存是同一块内存。\n\n> 所以有可能我们游戏占用的内存并不大，但是依旧爆内存了，其实是因为显存分配不出来了。这种情况，我们可以去查看一下Log，例如Android会有一个 OpenGL Error：Out Of Memory。\n\n  * 移动设备的CPU面积更小，因此会导致 **缓存级数更少** ，大小也更小，例如一般的台式机三级缓存可能有8-16M，而移动设备则只有2M左右。\n\n## 内存分页\n\n内存分页（IOS上大部分为16K/page）是内存管理的最小单位，当Unity需要申请内存的时候，都会以block的方式（若干个页）进行申请。如果某一页在若干次GC（IOS为8次）之后仍然为Empty的话，它就会被释放，实际的物理内存就会被还给物理内存。\n\n## Unity\n\n## Ref\n\n<https://zhuanlan.zhihu.com/p/370467923>\n<https://zhuanlan.zhihu.com/p/362941227>\n"
}
