目录

Memory Basics

Basic Concepts

Memory composition

The preceding figure shows the relationship among basic memory components (RSS, PSS, VSS, physical memory, and swap). The following table details the memory components.

Name

Description

Relevance/Functionality

Relationship

VSS (Virtual Set Size)

Size of all virtual memory occupied by processes, including the allocated but not used virtual memory.

Indicates the maximum address space that can be used by processes.

VSS consists of RSS and virtual memory that has been mapped to but not loaded into the physical memory, for example, the mapping through mmap or the reserved heap space.

RSS (Resident Set Size)

Process memory that is currently loaded into the physical memory, including the private memory and shared memory.

Displays the physical memory occupied by processes.

RSS consists of USS and shared memory.

PSS (Proportional Set Size)

Size of the memory after proportional allocation. The shared memory is calculated based on the average number of processes involved in sharing.

Reflects the memory usage in a multi-process environment in a more accurate manner.

PSS consists of USS and ∑, which is calculated as follows: Size of a shared page/Number of processes sharing the page.

USS (Unique Set Size)

Private memory that belongs only to the process and does not contain any shared part

Indicates the actual physical memory that can be released after the process is killed.

-

Graph

Graphics memory, that is, the DMA memory.

-

-

VMA (Virtual Memory Area)

Core data structure of a contiguous virtual memory region with identical attributes within the process's virtual address space.

Represents the basic unit for the kernel to manage the virtual memory. It determines the memory permission, sharing, and file mapping attributes.

VSS is the sum of all VMA lengths, regardless of whether physical memory is allocated. The OS usually limits the number of VMAs. If the number of VMAs exceeds the limit, new virtual address mappings cannot be created.

Ashmem (Anonymous Shared Memory)

Anonymous shared memory buffer with a pressure-aware reclamation mechanism.

Solves the sharing problem by using the pin/unpin mechanism to handle the memory overflow risk in the multi-process environment.

-

ION memory

Memory manager, which is used to allocate shared physical buffers between hardware.

Solves the memory sharing problem by using the buffer ownership transfer mechanism based on file descriptors.

-

GPU memory

Memory of the graphics processing unit.

Supports UI rendering, game models, video decoding display, and image processing filters.

On the mobile device with the unified buffer architecture (UMA), the GPU memory is a part of the system memory and is managed by the GPU driver.

Swap Partition (Swap)

Swap partition used to store the inactive memory pages that are moved by the system to the hard disk when the physical memory is insufficient.

Increases the available memory of the system, but the access speed is slow.

-

Shared Dirty Page

Shared dirty page that has been modified.

When it is shared by another process, you are advised to ensure access security.

-

Private Dirty Page

Private and modified memory area of a process, including stacks and heap memory that has been allocated but not released.

Belongs to a single process and cannot be shared with other processes.

-

Shared Clean Page

Unmodified (clean) pages shared by processes, for example, the read-only shared library code segment.

It can be shared by multiple processes and modifying one process does not affect other processes.

-

Private Clean Page

Private and unmodified data or code segments in a process, for example, the read-only text segment in an executable file.

It cannot be shared with other processes and has not been modified.

-

Anonymous Page

Data pages that are usually allocated by the heap or stack or through mmap and do not correspond to any specific file.

It may be swapped out if there is no sufficient physical memory.

-

File Backed Page

Data pages that correspond to a file and can be restored from the file system.

It can be directly discarded and reloaded, and does not need to be saved to the swap partition.

-

Private memory refers to a physical memory area that is exclusively owned and used by a specific process and cannot be directly shared or modified by other processes.

Shared memory is a memory management technology that allows multiple processes to access the same memory area simultaneously, enabling efficient data sharing and communication. The detailed explanation of shared memory is as follows:

Shared memory allows different processes to access the same memory space, reducing data replication and improving efficiency. It is implemented through memory-mapped files or by directly allocating shared memory segments.

Application Memory Composition

App memory composition

The preceding figure shows the basic components of the virtual memory space mapped by an app process. The following table details each component.

Name

Purpose

Allocation Time

Description

Stack

It stores function call stack frames, including local variables, parameters, and return addresses.

It is automatically allocated each time a function is called and automatically released after the function returns.

Direction: It grows downward (to a low address).

Restriction: The default size is limited (usually several MBs). Excessive recursion may cause stack overflow.

mmap allocation area

It is used for file mapping, anonymous mapping, shared memory, and large memory allocation.

It is allocated in the following scenarios: (1) The mmap system call is used. (2) The malloc is used to allocate large blocks of memory (exceeding the threshold). (3) The shmget is used to create shared memory. (4) The mmap is used to map file content to memory for reading and writing.

Advantage: It is flexible and supports on-demand loading and shared access.

Shared Libraries

It is used for loading dynamic link libraries, such as libc.so and libpthread.so.

It is loaded by the dynamic linker during program startup and loaded by dlopen during program running.

Feature: Multiple processes can share one physical page to save memory. It is included in the mmap allocation area.

Heap

It is used for dynamic memory allocation, such as malloc, calloc, and new operations.

It can be dynamically expanded as required during running.

Direction: It grows upward (to a high address).

Management: The brk and sbrk system calls are used to adjust the heap top.

Heap Alloc

It is the size of the heap memory that has been allocated and is being used.

Every time the program explicitly requests memory (for example, by using keyword new or similar memory allocation functions) and successfully obtains the required memory, the memory is considered as part of the allocated memory.

It directly reflects the actual heap memory used by the app.

Heap Free

It is the memory that has been allocated in the heap but has not been used yet.

At the initial allocation, all available heap space is considered "free". As the program runs, some free memory is occupied. The remaining free memory is Heap Free.

This part of memory can be allocated later. If it is not used for a long time, it may be wasted.

ArkTS Heap

It is the heap memory managed by ArkTS and used for storing dynamic data such as object instances and variables.

It is the heap memory allocated by ArkTS code.

-

Native Heap

It is the heap memory used by native code (for example, code written in C/C++), and is different from ArkTS Heap.

It occurs when the malloc, calloc, or realloc function or new operator is used to allocate memory. When operations involving underlying system resources are called, such as file opening and network connections, memory may be allocated in the native heap to store related resource descriptors or cached data.

-

Anonymous Page other (anonymous memory page)

It is the memory page not associated with file mapping.

It occurs when a program requests more stack space (for example, due to deep recursion) or allocates memory regions not associated with files through mmap. The dynamically allocated large blocks of memory (for example, those directly obtained from the operating system through mmap) may also be anonymous pages.

These pages are often used for storing process-private data structures, such as the stack space or dynamically allocated memory blocks that cannot be shared by multiple processes as they are anonymous.

FilePage Other

This type of memory page is mapped from files, but does not belong to any specific category.

It is generated when an app maps file content to memory through mmap for fast access.

When an app reads a file or loads resources, the OS may map the file content to the memory for fast access.

GL (graphics memory)

It stores graphics resources, such as textures and frame buffers.

It is allocated in the graph library when graphics resources, such as textures, frame buffers, and vertex buffers are created.

-

Uninitialized data segment (BSS Segment)

It stores uninitialized global variables and static variables.

It is initialized to 0 when the process is started, for example, static int uninit_var.

-

Initialized data segment (Data Segment)

It stores initialized global variables and static variables.

It is initialized based on the executable file content when the process is started, for example, int global_var = 10.

-

Code segment (Text Segment)

It stores machine instructions (namely, the compiled binary codes) of the program.

It is read-only and executable, and is loaded by the kernel from the executable file when the process is started.

-

Read-only data segment (RO Data Segment)

It stores read-only data such as constant character strings and const variables.

It is loaded during startup and corresponds to .rodata segment in the executable file, for example, const char* str = "hello".

-

Kernel reserved area (Guard Pages)

It prevents stack overflow or unauthorized access to adjacent memory areas.

It is automatically added below the stack or between other key areas by the system.

Behavior: Accessing this area will trigger a segmentation fault.