You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
flowchart TD
A[Unity 运行时采集快照] --> B[SystemMemoryRegions<br/>Address/Size/Resident/Type/Name]
B --> C[EntriesMemoryMapCache<br/>构建地址空间层次结构]
C --> D[GetPointType<br/>Mapped → PointType.Mapped]
D --> E1[AllMemorySummaryModelBuilder<br/>Summary 汇总]
D --> E2[AllTrackedMemoryModelBuilder<br/>All Of Memory 树形展示]
D --> E3[MemoryMapBreakdownModelBuilder<br/>Memory Map 着色]
publicenumMemoryType:ushort{// NB!: The same as in SystemInfoMemory.hPrivate=0,// Private to this process allocationsMapped=1,// Allocations mapped to a file (dll/exe/etc)Shared=2,// Shared memoryDevice=3,// Shared device or driver memory (like GPU, sound cards, etc)Count}
stuff like the first entry, which is the fake root for reporting the size of Executables & DLLS without any allocations actually rooted to it will remain with an invalid NativeObjectOrRootIndex and no sizes
[Unity][MemoryProfiler] Executables & Mapped 实现原理分析
整体数据流
flowchart TD A[Unity 运行时采集快照] --> B[SystemMemoryRegions<br/>Address/Size/Resident/Type/Name] B --> C[EntriesMemoryMapCache<br/>构建地址空间层次结构] C --> D[GetPointType<br/>Mapped → PointType.Mapped] D --> E1[AllMemorySummaryModelBuilder<br/>Summary 汇总] D --> E2[AllTrackedMemoryModelBuilder<br/>All Of Memory 树形展示] D --> E3[MemoryMapBreakdownModelBuilder<br/>Memory Map 着色]1. 数据源:OS 级 System Memory Regions
快照格式版本 ≥ 16 时,会包含
SystemMemoryRegions条目。类型枚举与 Unity 引擎SystemInfoMemory.h对齐:每个 region 包含:
RegionAddress/RegionSizeRegionResidentRegionTypeMapped = 1即文件映射)RegionName/path/to/libunity.so)由 Unity 运行时通过 OS API(如
VirtualQuery//proc/self/maps等)采集,Memory Profiler Editor 包只负责读取和展示。2. 内存地图:地址空间层次结构
EntriesMemoryMapCache把所有内存源叠成按地址排序的区间树:SystemMemoryRegion(OS 层,最底层)分类逻辑在
GetPointType中:对SystemMemoryRegion,读取RegionType,Mapped映射为PointType.Mapped:Android 特例:
/dev/路径归为 Device,dalvik相关归为 AndroidRuntime,不走 Mapped 分类。3. 汇总统计(Summary Tab)
AllMemorySummaryModelBuilder扁平扫描内存地图,按PointType累加:每个地址区间的
source决定其分类。DLL/可执行文件映射区通常没有 Unity 子分配,整段都标记为SystemMemoryRegion+Mapped;若 Mapped 区域内有 Unity 跟踪的分配,子区间会归为 Native/Managed,不会重复计入 Executables。4. 明细展示(All Of Memory Tab)
AllTrackedMemoryModelBuilder.ProcessSystemRegion按 region 名称分组:树结构示例:
子项名称来自 OS 的
RegionName(文件路径);同名 region 会合并大小。5. 与 Unity 内部分配体系的关系
Executables & Mapped 不在 Unity Memory Manager 跟踪范围内。
ProcessedNativeRoots注释提到一个用于 Executables/DLL 的 fake root,但它没有对应分配,不会计入大小:因此:
6. 驻留内存(Resident Memory)
有
SystemMemoryResidentPages时,ForEachFlatWithResidentSize会按 region 计算驻留大小。Executables & Mapped 可同时显示 Allocated 和 Resident(代码页可能被换出,resident < allocated)。7. 平台限制与 Legacy 行为
该指标并非所有平台一致报告:
SystemMemoryRegionsSystemMemoryRegions(旧快照/部分平台)TotalVirtualMemory估算 Untracked核心设计总结
RegionType == Mapped(文件映射)size = nextAddress - curAddressRegionName(文件路径)All Of Memory树、Memory Map 着色本质:Unity Memory Profiler 把 OS 已分类的"文件映射内存"单独成类,用于展示应用二进制和依赖库占用的虚拟/物理内存;它不深入分析代码段内容,只依赖 OS 的 region 元数据(地址、大小、类型、文件名)。
关键源码路径
Editor/MemorySnapshot/Cached/SystemMemoryRegionEntriesCache.cs— OS region 类型定义Editor/MemorySnapshot/Cached/EntriesMemoryMapCache.cs— 地址空间构建与GetPointTypeEditor/UI/Analysis/Breakdowns/Summary/Data/AllMemorySummaryModelBuilder.cs— Summary 汇总Editor/UI/Analysis/Breakdowns/AllTrackedMemory/Data/AllTrackedMemoryModelBuilder.cs— All Of Memory 树形展示Editor/MemorySnapshot/Cached/ProcessedNativeRoots.cs— fake root 说明(不计入大小)