Android

Android Memory Optimization: Offline Memory Testing

2025-10-27

The main purpose of offline memory testing is as follows.

Obtain memory metrics for the entire app and various scenarios, including virtual memory and physical memory.

When there is a memory exception, identify the specific type of memory exception, such as Java memory, Native memory, or which dynamic library exception.

Preliminary analysis of the causes of the problem.

Get the memory metrics of the app

To conduct memory testing, the first step is to know the overall memory situation of the current app, and then progress to specific memory indicators after an exception occurs. In Android, we can obtain overall memory metrics through the following methods.

Get the total virtual memory size and swap value:/doc/${pid}/status.

Get the memory usage of each type of process: dumpsys meminfo -- local ${pid}.

To obtain more detailed memory data:/pc/pid/maps, we can first use adbshellcat/pc/{pid}/maps,

Firstly, we can use adb shell cat/doc/pid/maps to obtain the total virtual memory size of the process, as shown in the following code.

//img.enjoy4fun.com/news_icon/d3vlko8fe6kc72ve7t20.png

As can be seen, through/doc/${pid}/status, we can obtain a lot of useful information, as shown below.

FDSize: Number of file descriptors. Some devices may crash when their file descriptor count exceeds the limit.

VmPeak: The peak value of virtual memory.

VmSize: The current virtual memory size.

VmSwap: Swap virtual memory size.

Threads: Number of threads.

Through these data, we can preliminarily obtain memory indicators, and then obtain data related to physical memory.

We can obtain the overall physical memory and detailed classification of physical memory data for the current app through adb shell dumpsys meminfo -- local

//img.enjoy4fun.com/news_icon/d3vll3r8hlms72v6ekb0.png

You can see dumpsys meninfo, which can obtain the overall physical memory situation and various types of physical memory information of the current app

Java Heap: Java Physical Memory

Naitve Heap: Naitive Physical Memory

Code: Physical memory for file mapping

Graphics: Physical memory, such as image and texture memory

TOTAL: Total physical memory

Java memory can be divided into Dalvik Heap and Dalvik Other

Graphics can also be divided into Gfx dev, EGL mtrack, GL mtrack

Retrieve the memory space data of the process

/Proc/pid/maps can provide us with detailed data on the virtual memory space of a certain process. proc/{pid}/maps can provide us with detailed data on the virtual memory space of a certain process

/Proc/pid/maps can provide us with detailed data on the virtual memory space of a certain process. Proc/{pid}/maps can obtain the overall virtual memory size and physical memory size of each memory of the process

Analyze memory usage

To analyze Java memory issues, generate a Prof file using adb shell am dump heap

//img.enjoy4fun.com/news_icon/d3vllnofe6kc72ve908g.png

Convert hprof con to. hprof file, and then use Android Studio Proflier to open and analyze it

more stories
See more