Analysis of Memory via Three Types of Charts

Question: What is untracked memory in Unity’s Memory Profiler? Do you know? Let’s explore this question together.

Three types of diagrams: Three different types of diagrams to look at memory.

Slide 1: ADB Memory Analysis Tool

adb tool

The following is further explanation and suggestions regarding memory usage for each part:

  • Java Heap (Java heap memory):
  • Primarily used for storing Java-related code, this memory usage is typically small in Unity projects. It mainly contains the memory used by the Java Virtual Machine (Dalvik) within the Unity application, suitable for storing runtime Java objects. In this report, the relatively small memory usage of the Java Heap indicates that Java-related resources and processing have a minimal impact on the application.
  • Native Heap(Native heap memory):
  • This portion of memory is used in C/C++ code via…mallocAllocated memory. Unity primarily uses this at the underlying level.VirtualAllocMemory allocation means that a lot of memory will be categorized asUnknownCategories, especially memory allocation for Mono virtual machines or il2cpp virtual machines, graphics API driver allocation, and allocation for third-party native plugins (such as Wwise).
  • This portion of memory can be further analyzed using tools like Perfetto, but Perfetto supports a limited number of devices. You can monitor this memory for abnormal growth or leaks, especially during long-term operation, as this may affect performance.
  • Code(Code memory):
  • This portion of memory represents the memory occupied by the application’s mapped code files (e.g., …).so、jar、apk、dex、oat、art(Files, etc.). In Unity projects, the primary source of code is…libunity.soandlibil2cpp.so。
  • The Android system uses a lazy loading mechanism for code files, loading only the relevant parts when needed. Therefore, the memory usage of this part is also related to the system’s memory state and may involve memory swapping. When analyzing, Private Dirty and Clean data should be referenced first for a more accurate understanding of actual memory usage.
  • Graphics(Graphics Memory):
  • This includes GPU-related memory such as mesh data and texture data. In Unity projects, this portion of memory typically accounts for a large proportion, especially in projects involving a large number of textures and meshes.
  • It’s important to note that some devices may exhibit statistical bias, misclassifying portions that should be categorized as graphics memory as “Unknown,” which can affect graphics resource analysis. In such cases, Unity Memory Profiler can be used for more accurate graphics memory analysis, helping to identify the specific sources of memory usage.
  • Unknown (Unknown memory location):
  • This is the most difficult memory region to track, containing anonymous memory allocations and memory allocations that cannot be directly categorized (such as memory allocations in C++, managed heaps in C#, and memory from third-party plugins).
  • This portion of memory may account for a significant portion of a Unity project’s memory usage and requires further analysis using the Unity Memory Profiler. The Profiler allows you to better pinpoint potential memory leaks or inappropriate memory usage.

Key points:

The pmap command provides us with very detailed information about each segment of the application’s memory location.

.so .art .oat .dex correspond to the memory of the Code section; dalvik corresponds to the memory of the Java section; and anon corresponds to the memory of the Unknown section.

Image 2: Unity Memory Profiler

Unity Memory Profiler

Unity Memory Profiler is a powerful tool that helps developers analyze memory usage in Unity projects, especially for Managed Heap and Native Heap. It tracks memory allocation information recorded by the Allocator and displays memory usage in different categories. Here are some key points and analyses:

1. Managed Heap

  • Managed Heap is the heap memory used by Unity C# code logic, primarily managed by the Mono or il2cpp virtual machine. You can analyze C# memory allocation through the “All Managed Objects” table in the “Objects and Allocations” tab.
  • In Use and Reserved are two states of memory:
    • “In Use” indicates memory that has been allocated and is still in its lifetime.
    • Reserved indicates that Unity has pre-requested memory space from the system. Although the system has not yet actually allocated this memory, it has reserved space for future memory expansion.

2. Native Heap

  • The Native Heap includes the memory allocation for all C++ level objects (such as GameObject, Transform, Collider, etc.). This part of the memory is often the easiest to overlook, but if there are a large number of Unity objects in the scene, it will occupy a lot of memory.
  • Native Allocations provides a detailed breakdown of this memory allocation, including:
    • Objects: Includes memory allocation for all Unity objects at the C++ level.
    • No Root Area: The memory used by the engine at its core, typically including internal buffers, arrays, and memory allocated by the graphics device module.
    • SerializedFile and PersistentManager.Remapper: Memory usage related to the loading of Asset Bundles (AB bundles), especially when the Prefab hierarchy in the AB bundle is deep, memory usage will increase significantly.

3. All Native Objects

  • This memory statistic includes all C++ objects with InstanceID, which typically consumes more memory than objects in Native Allocations. It also includes GPU memory for textures and meshes, causing its memory usage to differ from other categories.
  • The memory in the Overhead portion will be included in the Reserved portion, and may sometimes be underestimated.

4. Treemap

  • Treemap is a graphical memory analysis tool that helps developers understand their project’s memory usage more intuitively. It can display memory usage for Managed Heap, Native Heap, and Graphics, but it’s important to note that it doesn’t show all the details of memory usage within the Unity engine.
  • In some cases, especially memory issues related to AB package loading (such as memory differences when using Addressable and **AssetBundle.LoadFromFile()**), Treemap may not display complete memory information.

5. Untracked Memory

  • Untracked memory refers to memory portions that Unity cannot track, including Native Plugins, Executable & DLLs, and the il2cpp runtime. This portion of memory is typically allocated at the system level and cannot be directly monitored by Unity.
  • In actual testing, Untracked Memory may display as Unknown, and due to errors in Reserved space, there may be significant deviations. The calculation method for Untracked Memory is inaccurate and may be misleading; therefore, developers are advised not to rely entirely on data regarding this memory segment.

Key points:

The calculation method for Untracked Memory is inaccurate and may be misleading. The data displayed should be treated with caution to avoid being misled.

The third one, Perfetto

Analyzing Native Heap Memory

Perfect

The graph shows the Native Heap Profile obtained through Perfetto, which details the application’s memory allocation during runtime. The following are the key points analyzed in the graph:

Main content:

  • Total Malloc Size
  • The total amount of memory allocated is shown to be 226.49MB, which was allocated by different functions and operations.
  • Memory allocation stack:
  • Each line represents a memory allocation call stack, and different colors in the stack indicate different call paths.
  • For example,GfxDeviceWorker::Run、GfxDeviceProgram::RunCommand、CompileFragmentShaderThe above indicates that these memory allocations are related to graphics and rendering operations.
  • Android memory allocation:
  • The image contains multiple [related elements].androidThe related memory allocation shows memory operations related to internal Android system management (such as…).android::Surface::hook_queueBuffer、android::Vectorwait).
  • These operations involve memory allocation related to Android’s graphics, buffers, and resource management.
  • mallocMemory allocation:
  • Multiple locations are marked asmallocThis indicates that this memory is provided through the standardmallocFunction allocation.
  • For example,android::Surface::hook_queueBuffer、android::BufferQueueProducer::queueBufferAll of these involvemallocMemory allocation.
  • unknownLabel:
  • The image also shows multipleunknownThe label indicates that some memory allocation sources cannot be clearly categorized; they may be system-level or third-party library memory allocations.
  • Perfetto stack analysis:
  • Perfetto’s graphical analysis tool helps you clearly see the memory allocation call stack, from the application layer to the system layer, showing the upstream and downstream relationships of memory allocation.

Key points:

  • Performance bottleneck: If certain functions (such as…)mallocFrequent memory allocation, or memory allocation piling up at certain levels, can affect application performance, especially under high-frequency call conditions.
  • Android Memory Management: The diagram shows a large number of memory allocations related to Android graphics, buffers, and resource management, suggesting that you may need to optimize memory operations related to the Android graphics system or Surface.
  • Memory from unknown sources:unknownThe memory allocation under the tag needs further investigation, as it may involve memory usage that was not fully recognized by Perfetto, or memory allocation caused by third-party plugins.

Summarize:

These three types of graphs allow us to understand the application’s memory usage from different perspectives:

  • ADB memory analysis tools help us understand the distribution of memory and the proportion of memory used by different parts (Java, Native, Code, Graphics, Unknown), especially for memory management in the Android system.
  • Unity Memory Profiler focuses on managed heap and native heap memory in Unity projects and provides visualization tools (such as Treemap) to help analyze and optimize memory usage.
  • The Perfetto tool provides lower-level memory analysis, helping developers track the memory allocation stack, especially memory allocation related to graphics rendering and the Android system.
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇