目录

Resource Leak Detection

Overview

Resource leaks occur when resources such as handles, threads, and memory are not correctly released during application running. As a result, the resources are occupied for a long time and cannot be used by other applications. If a type of resources is used up, the system may freeze or restart. To address resource leak problems, the system provides the resource leak detection and determination, maintenance and debugging logs, and log reporting capabilities for you to locate faults. This document describes resource leak detection capability and log specifications.

Basic Concepts

Resource leaks consist of the following types: memory leaks, handle leaks, and thread leaks. The system periodically samples the resource usage of the process to detect these leaks. If the resource usage exceeds the threshold, the system captures the corresponding maintenance and debugging information and reports a leak event. Subscribe to the Hiappevent resource leak event.

Implementation Principles

The following table lists the resource leak detection methods.

Leak Type Detection Mechanism
Handle leak (FD_LEAK) Traverse the processes every 60 seconds to obtain the total number of FDs. If the number of FDs exceeds the threshold (5000), detailed handle information is captured and a handle leak is reported.
Thread leak (THREAD_LEAK) Traverse the processes every 60 seconds to obtain the total number of threads. If the number of threads exceeds the threshold (700), thread names are captured and a thread leak is reported.
Memory Leak (MEMORY_LEAK) - JS Leak (JS_LEAK) Insert instrumentation code within the JVM. When the heap usage exceeds 85% or out of memory (OOM) is triggered, a heap dump is captured and a JS leak is reported.
Memory Leak (MEMORY_LEAK) - Native Memory Leak (PSS_MEMORY) Monitor the application dynamic peak memory every 200 seconds. If the dynamic peak memory exceeds twice that of the average dynamic peak memory of applications, a memory leak is reported and memory control is triggered.
Memory leak, such as (MEMORY_LEAK) - ashmem/ion/gpu memory leak (KERNEL_MEMORY) If the anonymous shared memory (ashmem), ION or GPU memory usage exceeds the baseline value, a memory leak is reported and maintenance and debugging information is captured.

Note:

  1. The thresholds and baselines in the table are default values. If the ecosystem needs to set a baseline during development, you can call the hidebug.setAppResourceLimit API. It is recommended that this API be called in the development phase instead of the official release phase.

  2. VM memory usage = heapUsed/totalHeap

    heapUsed: size of the heap used by the current VM, in KB. You can obtain the value through hidebug.getAppVMMemoryInfo().

    totalHeap: total size of the heap of the current VM, in KB. You can obtain the value through the hidebug.getAppVMMemoryInfo() interface.

  3. When an application reports a JS_ERROR/CPP_CRASH fault and the error message contains "OutOfMemory", locate the fault by referring to Memory Leak Analysis Method.

  4. Management and control means that the system proactively terminates the behavior of leaking an application after determining that the application is leaked.

The resource leak kernel is controlled as follows:

In HarmonyOS 6.1.0 and later versions, the system resources are insufficient due to fast application resource application or foreground leak. For example, the quick application (RSS, ion, and GPU memory) of a single application exceeds 1/3 of the system memory. As a result, the memory of the system is low, and the system controls the application, some basic RESOURCE_OVERLIMIT maintenance and debugging information is provided.

Description

  1. Currently, the following resource types support kernel control: ion, gpu, rss, ashmem, and thread.

  2. The kernel control does not distinguish the foreground and background. As a result, the foreground of the faulty app may exit unexpectedly.

Constraints

  1. The maintenance and debugging of the handle leak call stack, native memory leak call stack, and JS leak memory snapshot are disabled by default in the no-log version.

  2. If you want to obtain the JS memory leak snapshot of the no-log version, add the subscription to the JS memory snapshot of the no-log version by referring to Resource Leak Event Subscription (ArkTS).

Obtaining Logs

Resource leak logs are managed by the LeakDetector module and can be obtained in any of the following ways:

Handle Leak Log Specifications

Fault log: [pid]_fd_leak.txt (Method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (Method 3)

[h2]Header Information

Field name. Description
time Fault occurrence time.
pid PID of the failed process, which can be used to search for related process information in the HiLog logs.
process Application process package name.
leaked fd nums Number of leaked FDs (using snapshot).
time: 2024/06/27 11:55:28
pid: 1380
process: process1
leaked fd nums: 5111

[h2]Detailed Handle Type Information

[h2]Maintenance and Debugging Information About Special Handles

If Leaked fd Top 10 contains information about the ashmem, socket, pipe, sync_file, or dmabuf type and the number of handles of this type exceeds 1000, detailed maintenance and debugging information about the entire system is also provided. The details are as follows:

[h2]Handle Stack Information

When a handle leak occurs, the system calls such as pipe and open of the process are hooked for 10 minutes to capture call stacks, which are sorted by type. Each line in the following example is a call stack. The stacks are called from right to left. The number following num indicates the total number of call stacks, and the list following bt indicates the specific call stacks. The specific stack information can be parsed to the corresponding function through addr2line.

*****************************
LOGGER_MEMCHECK_FD_STACK_INFO
pid: 12326
get stack time: 2024/06/17 19:16:48
==============================FdTrack Stack==============================
Generated by HiviewDFX @HarmonyOS
==============================Sorted by num==============================
num 8272 bt [/system/lib64/libfdleak_tracker.so+0x1fb58] [/system/lib/ld-musl-aarch64.so.1+0x1d3154] [/system/lib/ld-musl-aarch64.so.1+0x148940] [/system/lib64/platformsdk/libuv.so+0x1ab30] [/system/lib64/platformsdk/libuv.so+0x1cbd0] [/system/lib64/module/file/libfs.z.so+0x17109c] [/system/lib64/module/file/libfs.z.so+0x170af4] [/system/lib64/module/file/libfs.z.so+0x1701c8] [/system/lib64/platformsdk/libace_napi.z.so+0x34828]
num 3968 bt [/system/lib64/libfdleak_tracker.so+0x1fb58] [/system/lib/ld-musl-aarch64.so.1+0x1d3154] [/system/lib64/platformsdk/libipc_core.z.so+0x4ac64] [/system/lib64/platformsdk/libbackup_kit_inner.z.so+0x532d4] [/system/lib64/platformsdk/libbackup_kit_inner.z.so+0x4f8fc] [/system/lib64/platformsdk/libipc_core.z.so+0x38420] [/system/lib64/platformsdk/libipc_core.z.so+0x4e99c] [/system/lib64/platformsdk/libipc_core.z.so+0x4eb34] [/system/lib64/platformsdk/libipc_core.z.so+0x4edc8]
num 3968 bt [/system/lib64/libfdleak_tracker.so+0x1fb58] [/system/lib/ld-musl-aarch64.so.1+0x1d3154] [/system/lib64/platformsdk/libipc_core.z.so+0x4ac64] [/system/lib64/platformsdk/libbackup_kit_inner.z.so+0x532b0] [/system/lib64/platformsdk/libbackup_kit_inner.z.so+0x4f8fc] [/system/lib64/platformsdk/libipc_core.z.so+0x38420] [/system/lib64/platformsdk/libipc_core.z.so+0x4e99c] [/system/lib64/platformsdk/libipc_core.z.so+0x4eb34] [/system/lib64/platformsdk/libipc_core.z.so+0x4edc8]
  1. All the call stacks that apply for handles within 10 minutes are collected, including the closed call stacks.

  2. Stack information is available only in the log version. If Developer options is not enabled in the nolog version, stack information is not captured. If stack information is not captured, enable System resource leak log in Developer options and restart the device to capture stack information.

[h2]Clustering Rules for Handle Leak

Leak issues generated by an application at different times on the same or different versions may be caused by the same reason. You can extract fault characteristics from fault logs and cluster multiple fault logs based on the fault characteristics to improve the analysis efficiency of leak issues.

  1. Determine the type of handle leak based on the number of top 10 file handles and the number of top 10 file path handles in Handle Leak Log. Use the handle type that accounts for the largest proportion as the fault characteristic.

  2. For special types of handles such as ashmem and ion, refer to the ASHMEM_LEAK and ION_LEAK rules for clustering.

Thread Leak Log Specifications

Fault log: [pid]_thread_leak.txt (Method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (Method 3).

[h2]Header Information

Field name. Description
time Time when thread leak is detected.
pid PID of the failed process, which can be used to search for related process information in the HiLog logs.
vss All the address spaces that are accessible to a process, including the part that has not yet resided in memory. (Unit: KB)
rss Memory size actually occupied by a process, including the total memory size of the shared library used by the process. (Unit: KB)
process Bundle name of the faulty application.
summary Total number of process threads when a leak is identified.
time: 2024/06/27 03:45:19
pid: 41897
vss: 12783644
rss: 2229352
process: process1
summary: 879

[h2]Thread Leak Information

[h2]Clustering Rules for Thread Leaks

Leak problems generated by applications on the same or different versions at different time may be caused by the same cause. Developers can extract fault characteristics from fault logs and cluster multiple fault logs based on the fault characteristics to improve the efficiency of analyzing leak problems.

  1. Analyze the top 10 threads displayed in Thread Leak Log. The name of the thread that accounts for the largest proportion is used as the fault characteristic.

    The system has clustered threads with the same name. However, in some special scenarios, thread names may be different, such as IPC_11_13795 and IPC_12_13796. If you want to further enhance the clustering effect, you can use similar process names in all threads as the same fault characteristic, such as IPC_xx_xxx, to improve clustering accuracy.

  2. Extracts the thread snapshot corresponding to the thread from the thread leak log and performs clustering based on the call stack.

JS Memory Leak Log Specifications

Fault log: memleak-js-[process_name]-[pid]-[tid]-[timestamp].rawheap (Method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (Method 3)

[h2]Clustering Rules for JS Leaks

Leak problems generated by applications on the same or different versions at different time may be caused by the same cause. Developers can extract fault characteristics from fault logs and cluster multiple fault logs based on the fault characteristics to improve the efficiency of analyzing leak problems.

Description of the clustering procedure

  1. Confirm the clustering scope. Only the top 20 directories and directories whose retained size accounts for more than 5% are clustered.

  2. Cluster the faults reported based on the clustering rules. To ensure the validity of the clustering result, the objects stored in different directories are different, and the clustering rules are different.

  3. Collect statistics on the number and memory size of each type of objects, sort the objects by the number or memory size, and find the object with the highest memory usage as the fault feature of the reported fault.

Clustering rule

There are three types of clustering rules: name clustering, reference chain clustering, and attribute clustering. Different types of objects require different clustering methods.

  1. Clustering by object name

    Objects in the Function(closure) directory are clustered by object name. Objects with the same name are considered as one type.

  2. Clustering through the shortest reference chain of an object

    Objects in directories such as Method, js_set, Map, string, and JSNativePointer are preferentially clustered through reference chains. The names of this type of objects in the directory are the same. The reference chain can be used to distinguish object creation scenarios. Compares the shortest reference chain from each object in the directory to the GC root. If there are multiple shortest reference chains, the reference chain with the largest retained size is selected. If the shortest reference chains are the same, they are considered as one type.

  3. Object name + reference chain clustering

    Service objects (class objects and function objects created on the service side) contain path object names and line numbers. If the call points of the same class or function object are different, the reference chains are different. You need to distinguish the reference chains based on the call points.

    In addition, framework and jsarray(array) are clustered by object name and reference chain.

  4. Attribute + reference chain clustering

    The objects in the jsobject (js_shared_object) directory have the same name but represent different objects. You need to use the reference chain to classify the jsobject objects, that is, distinguish the jsobject objects of different call points.

    There are a large number of objects whose distance is 1 in the jsobject object. These objects cannot be distinguished by reference chains, but can be distinguished by attributes. That is, whether the objects directly held by the jsobject object are consistent. If the names of the objects directly held by the jsobject object are the same and the holding relationships are the same, the objects are classified into one type.

  5. Special clustering

    5.1 SourceTextModule

    The SourceTextModule object is created on the system side and is used to hold the export object. A SourceTextModule object is generated for each TS file. The SourceTextModule object holds the exported object in the TS file. Therefore, the SourceTextModule object is the result after clustering and does not need to be clustered again.

    5.2 Promise/PromiseRecord/PromiseCapability/PromiseReaction

    Objects related to asynchronization. Perform clustering based on the handle Message+ reference chain under PromiseReaction. The handle information under PromiseReaction indicates the previous task of the asynchronous task, which can be distinguished by the previous task.

    5.3 proxy

    Proxy is often closely related to framework objects and can be clustered using the target information (handle) and reference chain under proxy. The target information under proxy indicates the proxied object held by the proxy. The proxy is meaningless and needs to be distinguished based on the proxied object.

  6. Other basic types except the preceding types:

    By default, objects of other basic types are clustered based on the shortest reference chain.

native Memory Leak Log Specifications

Fault log: RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (Method 3) or memleak-native-[process_name]-[pid]-sample.txt (Method 1).

[h2]Memory Sampling

The memleak-native-[process_name]-[pid]-sample.txt file contains the process ID, process name, baseline value, and memory sampling information, in which you can view the memory change.

Field name. Description
SoftThreshold Process threshold set by the system (or set by the application using setAppResourceLimit()), in KB. If the application memory usage exceeds the threshold for five consecutive times, a memory leak event is reported.
HardThreshold Process hard threshold set by the system, in KB. If the application memory usage exceeds the hard threshold for two consecutive times, a memory leak event is reported.
PSS Proportional resident memory size, in KB. The memory size of the shared library is evenly allocated based on the number of processes. The PSS usage of a process is accurately calculated at the time marked with an asterisk (*).
Offset Offset of the allocation of the shared library, in KB. You can estimate the PSS value without asterisks (*) by using the following formula: RSS + Offset = PSS.
RSS Total resident memory size of the process in the physical memory (including the total memory occupied by the shared library), in KB.
SwapPSS Total memory size that is written to the swap space by the process, in KB.
TotalPSS Total PSS usage of the process, in KB.
AvcMem Memory created by the process when the process creates a codec instance through Avcodec_service. The memory is reported by Avcodec_service to HiView for statistics collection. The unit is KB.
MediaMem Memory created by the process through the Media_service API. The memory is reported by Media_service to HiView for statistics collection. The unit is KB.
GPU GPU memory used by the process, in KB.
ION ION memory used by the process, in KB.
TotalMem Total memory size used by the process, in KB. TotalMem = TotalPSS + ION + GPU + AvcMem + MediaMem.
Level Leak level of the process.
RunningTime Current lifecycle of the process, in seconds.
Realtime Real time of the sampling.
/*************************************************************
/*                  ***** READ ME *****                      *
/*************************************************************
/*                 RSS: Resident Set Size                    *
/*                 PSS: Proportional Set Size                *
/*                 RSS + Offset = PSS                        *
/*                 TotalPSS = PSS + SwapPSS                  *
/*   TotalMem = TotalPSS + Av_Mem + Media_Mem + ION + GPU    *
/*                 ***** Two Modes *****                     *
/*      Estimate Mode: RSS & SwapPSS is real                 *
/*      Real Mode(Realtime with *): everything is real       *
/*      Media_rss:apply mem through media_service            *
/*      Avc_rss:apply mem through avcodec_service            *
/*    ~ means negligible memory(safe to ignore in analysis)  *
/*************************************************************
/*                   ***** Attention *****                   *
/*    Formulas about TotalMem and sub-items may change,      *
/*    please reference current annotation formula            *
/*************************************************************

pid: XXXX
processName: XXXXXX
SoftThreshold: 3500(KB)
HardThreshold: 1024000(KB)

Index   RSS(KB)     Offset(KB)  PSS(KB)     SwapPSS(KB)     TotalPSS(KB)     MediaMem(KB)   AvcMem(KB)    GPU(KB)       ION(KB)       TotalMem(KB)     Level   RunningTime(s)     Realtime
1       14668       0           14668       5500            20168            ~              ~             ~             ~             20168            W       112             2025/04/23 12:28:02
2       12732       0           12732       5476            18208            ~              ~             ~             ~             18208            W       352             2025/04/23 12:32:01
3       13560       0           13560       5456            19016            ~              ~             ~             ~             19016            W       592             2025/04/23 12:36:02
4       13576       0           13576       5440            19016            ~              ~             ~             ~             19016            W       832             2025/04/23 12:40:02
5       13576       0           13576       5440            19016            ~              ~             ~             ~             19016            W       1072            2025/04/23 12:44:02
6       13584       -8320       5264        5440            10704            ~              ~             ~             ~             10704            W       1072            *2025/04/23 12:44:02
7       12984       -8320       4664        5084            9748             ~              ~             ~             ~             9748             W       1312            2025/04/23 12:48:02

[h2]Memory Maintenance and Debugging

Log file: memleak-native-[process_name]-[pid]-smaps.txt

Field name. Description
RealPssMemory PSS value collected at realtime, in KB.
LOGGER_MEMCHECK_MEMINFO Memory information of the device, including MemTotal and MemFree.
LOGGER_MEMCHECK_SMAPS_INFO smaps information of the process.
LOGGER_MEMCHECK_SAMPLE_NMD_INFO Information about two jemalloc allocations of the process (the interval between the two records is 5 minutes). The system captures the memory stack based on this information.
LOGGER_MEMCHECK_DETIAL_INFO jemalloc snapshot details of the process.
Generated by HiviewDFX @HarmonyOS
LOGGER_MEMCHECK_GERNAL_INFO
 pidNumber: 2017
 processName: process1
 PidStartTime: 1602
 RealPssMemory: 83505

*****************************
LOGGER_MEMCHECK_MEMINFO
MemTotal:                             11332540 kB
MemFree:                               1686056 kB
......

LOGGER_MEMCHECK_SMAPS_INFO
-------------------------------[memory]-------------------------------
                                    Shared      Shared      Private     Private
Size        Rss         Pss         Clean       Dirty       Clean       Dirty       Swap        SwapPss     Counts                        Name
2048        0           0           0           0           0           0           0           0           1                             /dev/__parameters__/param_sec_dac
...
45778196    744068      827123      213560      55288       403304      71916       308888      308888      6500                          Summary
......

LOGGER_MEMCHECK_SAMPLE_NMD_INFO

            size       allocated         nmalloc         ndalloc

               8           17384          511848          509675
              16          129376          338438          330352
              32         1138816         1026155          990567
              48         3161808         1322095         1256224
              64         1869376          908151          878942
......

************ endl ************

LOGGER_MEMCHECK_SAMPLE_NMD_INFO

            size       allocated         nmalloc         ndalloc

               8           17384          511848          509675
              16          129376          338438          330352
              32         1138816         1026155          990567
              48         3161808         1322095         1256224
              64         1869376          908151          878942
......

************ endl ************

*****************************
LOGGER_MEMCHECK_PROC_INFO
ASHMEM_PROCESS_INFO
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
Process_name Process_ID Fd Cnode_idx Applicant_Pid Ashmem_name Virtual_size Physical_size magic
XXXXX           816             22      328234  816     dev/ashmem/PolicyVolumeMap      541             4096            7
************ endl ************

******************************
LOGGER_MEMCHECK_DETIAL_INFO
allocated         nmalloc   (#/sec)         ndalloc   (#/sec)       nrequests   (#/sec)           nfill   (#/sec)          nflush   (#/sec)
small:                      183785560        12591759       619        10371251       510         1289491        63         1313204        64          956094        47
large:                       31059968            3359         0            2946         0            3359         0            3359         0               0         0
total:                      214845528        12595118       619        10374197       510         1292850        63         1316563        64          956094        47

......

bins:           size ind    allocated      nmalloc (#/sec)      ndalloc (#/sec)    nrequests   (#/sec)  nshards      curregs     curslabs  nonfull_slabs regs pgs   util       nfills (#/sec)     nflushes (#/sec)       nslabs     nreslabs (#/sec)      n_lock_ops (#/sec)       n_waiting (#/sec)      n_spin_acq (#/sec)  n_owner_switch (#/sec)   total_wait_ns   (#/sec)     max_wait_ns  max_n_thds
8   0       198920       163820       8       138955       6       119703         5        1        24865           56             19  512   1  0.867         6526       0         4008       0           96        26995       1           10990       0               0       0               0       0            1226       0               0         0               0           0
16   1      1802688      1143707      56      1031039      50       221165        10        1       112668          563            309  256   1  0.781       105471       5        82548       4         1942        80503       3          191126       9               0       0              14       0            4316       0               0         0               0           0
32   2      9954560      1867465      91      1556385      76       267993        13        1       311080         2614            503  128   1  0.929       177825       8       136745       6         7713       176923       8          325128      15               2       0              52       0            8139       0               0         0               0           1
48   3     35382816      3763756     185      3026614     148       300952        14        1       737142         2953            220  256   3  0.975       371881      18       283650      13        12022        60637       2          667997      32               2       0              17       0            2725       0               0         0               0           1
......

Note:

LOGGER_MEMCHECK_SAMPLE_NMD_INFO and LOGGER_MEMCHECK_DETIAL_INFO are jemalloc snapshots of a process. The differences are as follows:

  1. LOGGER_MEMCHECK_SAMPLE_NMD_INFO: The maintenance and debugging sampling is performed twice consecutively at an interval of 5 minutes. The content contains four columns of memory application information, including size, allocated, nmalloc, and ndalloc.

  2. LOGGER_MEMCHECK_DETIAL_INFO: The maintenance and debugging sampling is performed only once. The content contains complete jemalloc information of the process.

[h2]Memory Stack

Stack information log file: memleak-native-[process_name]-[pid]-[timestamp].txt

[h2]native leak clustering rules

Leak problems generated by applications on the same or different versions at different time may be caused by the same cause. Developers can extract fault characteristics from fault logs and cluster multiple fault logs based on the fault characteristics to improve the efficiency of analyzing leak problems.

For a PSS_MEMORY fault, the system sends two fault logs to developers for analysis. Fault log 1 (memory maintenance and debugging) is used to store some lightweight information about the process, such as smaps, ashmem, and NMD. Fault log 2 (memory stack) is used to store stack information captured after the system detects a process pss leak.

  1. Analyze the LOGGER_MEMCHECK_SMAPS_INFO information of the process in fault log 1, calculate the memory usage of each type based on the methods listed in the following table, and filter out the memory leak type with the highest memory usage.

    Memory Leak Type Calculation Method
    VM object leak Search for the keyword "ArkTS" and calculate the percentage of the sum of the Pss and SwapPss columns to the total memory.
    Heap memory leak Search for the keyword "jemalloc" and calculate the percentage of the sum of the Pss and SwapPss columns to the total memory.
    ashmem memory leak: Search for the keyword "/dev/ashmem" and calculate the percentage of the sum of the Pss and SwapPss columns to the total memory.
    Large anon memory Search for the keyword "[anon]" and calculate the percentage of the sum of the values in the Pss and SwapPss columns to the total memory.

    Note:

    1. The sum of the Pss and SwapPss columns in each row is the requested size of the corresponding memory block.

    2. The Name column indicates the memory label of the memory block.

  2. After the memory leak type with the highest proportion is filtered out, you can further extract the fault characteristics based on the following rules:

    • VM object: Filter the memory label with the largest memory application as the fault characteristic. For example, the fault label extracted from the following log is [anon:ArkTS Heapsemi space].

      LOGGER_MEMCHECK_SMAPS_INFO
      -------------------------------[memory]-------------------------------
                                          Shared      Shared      Private     Private
      Size        Rss         Pss        Clean       Dirty         Clean       Dirty       Swap        SwapPss     Counts                 Name
      1662976     68744       68744       0           0           68744       0           0           0           525                     [anon:ArkTS Heap]
      
      5632        5600        2261        3452        0           2148        0           0           0           22                      [anon:ArkTS Heapappspawn space]
      
      98304       74012       74012       0           0           74012       0           0           0           307                     [anon:ArkTS Heapsemi space]
      
      ...
    • Heap memory: Filter the memory label with the maximum memory application as the fault characteristic. For example, the fault label extracted from the following log is [anon:native_heap:jemalloc].

      LOGGER_MEMCHECK_SMAPS_INFO
      -------------------------------[memory]-------------------------------
                                          Shared      Shared      Private     Private
      Size        Rss         Pss        Clean       Dirty         Clean       Dirty       Swap        SwapPss     Counts                 Name
      327168      171464      146921      25416       0           146048      0           0           0           17                      [anon:native_heap:jemalloc]
      24600       3924        3586        348         0           3576        0           0           0           18                      [anon:native_heap:jemalloc meta]

      If a memory call stack exists, find the memory range with the highest usage based on the NMD maintenance and debugging, and cluster the captured call stack maintenance and debugging to the specific code snippet or so. For details, see Native Leak Analysis Method.

    • ashmem memory: See ashmem clustering rules.

    • anon memory: Filter the memory label with the largest memory application as the fault characteristic. For example, the fault label extracted from the following log is [anon].

      LOGGER_MEMCHECK_SMAPS_INFO
      -------------------------------[memory]-------------------------------
                                          Shared      Shared      Private     Private
      Size        Rss         Pss        Clean       Dirty         Clean       Dirty       Swap        SwapPss     Counts                 Name
      38760       528         524         4           0           524         0           0           0           56                      [anon]

      If a memory call stack exists, analyze the clustering that accounts for a large proportion in the call stack to a specific code snippet or so. For details, see Native Leak Analysis Method.

    For the no-log version, stack capture is disabled by default. Therefore, you are advised to perform preliminary clustering on the PSS memory usage types based on the smaps information.

Log Specifications of ashmem/ION/GPU/GPU_RS Memory Leaks

[h2]Memory Sampling

[h2]Memory Maintenance and Debugging

Log file: memleak-kernel-[module]-0-[timestamp].txt (Method 1).

Field name. Description
LOGGER_MEMCHECK_MEMINFO Overview of the device memory information.
LOGGER_MEMCHECK_PROC_INFO Information about the ashmem, ION, or GPU memory leak node. (The content varies according to the leak type.)
LOGGER_PROCESS_DMABUF_INFO Special node content obtained when the ION memory leak occurs, which contains more memory block usage information.
LOGGER_MEMCHECK_RENDER_SERVICE_MEM Memory usage of the Render_service process.

When an ashmem, GPU or ION memory leak is detected, the ashmem, GPU or ION memory information of the entire device is captured. The specifications of ashmem and ION logs are the same as those of ashmem and dmabuf handle leak logs. For details, see ashmem and dmabuf handles.

Log header:

Generated by HiviewDFX @HarmonyOS
LOGGER_MEMCHECK_GERNAL_INFO
memoryName:ion
softThreshold:2800(MB)
hardThreshold:4200(MB)
appHardThreshold:4096(MB)
topMemory:0(KB)

*****************************
LOGGER_MEMCHECK_MEMINFO
MemTotal:       11738500 kB
MemFree:          116204 kB
MemAvailable:      95232 kB
Buffers:               0 kB

LOGGER_MEMCHECK_PROC_INFO in the log file records the memory information based on the memory leak type. The details are as follows:

Note:

  1. Differences between the GPU_RS memory leak and the GPU memory leak: The GPU memory leak is caused by application self-rendering, while the GPU_RS memory leak is caused by unified rendering through the render_service process.

  2. In the resource leak information, ION, dmaheap, and dmabuf can be regarded as the same memory type.

  3. The current log specifications may change in rows, columns, and paragraphs based on version issues and user feedback.

[h2]Memory Stack

Since HarmonyOS 6.0.0, the call stack of GPU memory allocation can be captured to analyze GPU memory leaks in processes. After a leak is detected, the GPU memory allocation trace within 15 minutes is collected. You can set up a local Smartperf environment and import the Profiler logs for parsing.

Log file name: memleak-kernel-[module]-[pid]-[timestamp].txt

[h2]ashmem/ion/gpu/gpu_rs clustering rules

Leakage issues of an application generated at different times in the same or different versions may be caused by the same reason. You can extract fault characteristics from fault logs and cluster multiple fault logs based on the fault characteristics to improve the analysis efficiency of leakage issues.

ashmem clustering rules

  1. Extract the ashmem maintenance and debugging information from the fault logs for clustering.

    Process_name    Process_ID      Fd      Cnode_idx       Applicant_Pid   Ashmem_name                     Virtual_size    Physical_size  magic
    XXXXX           816             22      328234          816             dev/ashmem/PolicyVolumeMap      541             4096            7
    XXXXX           816             22      328234          816             dev/ashmem/PolicyVolumeMap      541             4096            7
    XXXXX           816             22      328234          816             dev/ashmem/inputBuffer          541             1024            7
    XXXXX           816             22      328234          816             dev/ashmem/outputBuffer         541             512             7
    ......
  2. Analyze the Ashmem_name field in ashmem maintenance and debugging to check whether the buffer leak is of the same type. Use the buffer with the highest sum of values in the Physical_size column as the fault feature.

ion clustering rules

  1. Extract the ion maintenance and debugging information from the fault logs for clustering.

    *****************************
    LOGGER_MEMCHECK_PROC_INFO
    MM_DMABUF_INFO
    Process     pid      fd         size_bytes    ino        exp_pid      exp_task_comm    buf_name                                                                         exp_name           buf_type    leak_type
    process1    65141    246        278528        432510     42829        allocator_host    65141                                                                           mm_heap_helpers    NULL        NULL
    process1    65141    247        266240        434225     42829        allocator_host    65141                                                                           mm_heap_helpers    NULL        NULL
    process1    65141    248        274432        430933     42829        allocator_host    65141                                                                           mm_heap_helpers    NULL        NULL
    process1    65141    264        14036992      432500     42829        allocator_host    https://xxxx/xxxxx.png                                                          mm_heap_helpers    pixelmap    pixelmap
    process1    65141    266        14036992      426988     42829        allocator_host    https://xxxx/xxxxx.png                                                          mm_heap_helpers    pixelmap    pixelmap
    process1    65141    268        14036992      430936     42829        allocator_host    https://xxxx/xxxxx.png                                                          mm_heap_helpers    pixelmap    pixelmap
    process1    65141    258        4493312       432499     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    260        4493312       426987     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    262        4493312       431689     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    254        4493312       430935     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    256        4493312       431688     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    250        4493312       432498     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    process1    65141    252        4493312       430934     42829        allocator_host    srcImageSize-2160x2880-pixelMapSize-2160x2880-streamsize-761322-mimetype-webp   mm_heap_helpers    NULL        NULL
    ************ endl ************
  2. Classify buffers based on leak_type and buf_name, and extract the buf_name of the buffer with the highest proportion of the sum of size_bytes as the fault feature. For details about the analysis, see ION Leak Analysis Method.

    Note:

    If leak_type is NULL, there may be multiple types of buffers, which can be distinguished by buf_name.

gpu/gpu_rs clustering rules

  1. Parse the GPU maintenance and debugging information provided in the fault log, collect statistics on the lines that contain the "Total memory:" field, parse the GPU memory usage of the corresponding type, and select the GPU memory type with the highest usage as the fault feature. For example, in the following maintenance and debugging information, the GPU memory usage of the host default memory type is 1789351 bytes, which can be used as a fault feature for clustering.

    C: cq memory(not in total memory) (Total memory: 4096)
    13:                    1 / 4096
    
    C: vulkan external memory(not in total memory) (Total memory: 0)
    (empty)
    
    C: host default memory (Total memory: 1789351)
      5:                  269 / 4304
      6:                  370 / 15792
      7:                  401 / 33088
      8:                   96 / 14240
      9:                  449 / 141696
      10:                  265 / 173143
      11:                  172 / 236391
      12:                   68 / 187012
      13:                   35 / 197576
      14:                   17 / 251573
      15:                    6 / 108304
      16:                    1 / 51736
      17:                    1 / 84184
      19:                    1 / 290312
  2. After clustering to the GPU memory type, the memory segment with the largest usage is filtered out and clustering is performed.

The GPU representation varies depending on the chip platform.

C: host default memory (Total memory: 1789351).

Channel: Texture (Total memory: 1789351).

Kernel Management and Control Log Specifications

When kernel control is triggered, the system resources are insufficient to support the capture of maintenance and debugging information with high overhead. Therefore, only basic maintenance and debugging information can be provided to support preliminary demarcation and locating.

The general content of kernel control logs is as follows:

Field name. Description
pidNumber PID of the leaked application.
processName Leaked application process name.
killTime Time when an application is killed.
RssMemory RSS memory usage of the leaked application, in KB.
killReason Reason why the app is killed.
Generated by HiviewDFX @HarmonyOS
LOGGER_MEMCHECK_GERNAL_INFO
 pidNumber: 2017
 processName: process1
 killTime: 20260512090030
 killReason: RssThresholdKiller
 RssMemory: 8350500

Description

RssMemory indicates the total RSS memory size displayed in the log after the application triggers RSS kernel control. Similarly, GpuMemory, IonMemory, and AshmemMemory indicate the memory size displayed in the kernel control logs.

[h2]RSS Kernel Control

Overview: When the RSS memory leak of an application exceeds 1/3 of the running memory of the entire system and the memory of the entire system is low, the RSS kernel control is triggered.

Log file: memleak-kernel-[module]-0-[timestamp].txt (method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (method 3)

The content of the RSS kernel management and control log is as follows:

Field name. Description
LOGGER_MEMCHECK_SAMPLIFY_SMAPS_INFO Records the SMAPS summary information about the process.
Size Virtual memory size of the memory block requested by the application, in KB.
Rss Physical memory size of the memory block applied for by the application, in KB.
Swap Size of the physical memory that is allocated to the SWAP partition and is not used by the application, in KB.
Name Memory label applied for by an application.
LOGGER_MEMCHECK_SAMPLIFY_SMAPS_INFO
    Size         Rss        Swap    Name
     256           4           0    [anon:ark-Object Space]
     256         240           0    [anon:ark-Non Movable Space]
     256          48           0    [anon:ark-Object Space]
  523520           4           0    [anon]
  783104           0           0    [anon:ArkTS Heap]
     256         144          16    [anon:ArkTS Heapnon movable space]
     256           8           0    [anon:ArkTS Heapread only space]
     256          32           0    [anon:ArkTS Heapshared non movable space]
     256          48           0    [anon:ArkTS Heapshared read only space]
     256         136          60    [anon:ArkTS Heapnon movable space]
  524288           0           0    [anon:ArkTS Heap]
     256          32           0    [anon:ArkTS Heapappspawn space]
     256         240          16    [anon:ArkTS Heapsemi space]
......

Description

You can calculate the total RSS memory usage of a process based on the sum of memory values in the RSS column.

[h2]GPU Kernel Control

Overview: When the GPU memory leak of an application exceeds 1/3 of the running memory of the entire system and the memory of the entire system is low, the GPU kernel control is triggered.

Log file: memleak-kernel-[module]-0-[timestamp].txt (method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (method 3)

The GPU kernel control log content is the same as the maintenance and debugging content under the LOGGER_MEMCHECK_PROC_INFO field in GPU Memory Leak Log Specifications.

[h2]ashmem Kernel Control

Overview: When the ashmem memory leak exceeds 1/3 of the running memory of the entire system and the memory of the entire system is low, the ashmem kernel control is triggered.

Log file: memleak-kernel-[module]-0-[timestamp].txt (method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (method 3)

The ashmem kernel management and control log is consistent with the maintenance and debugging information under the LOGGER_MEMCHECK_PROC_INFO field in ashmem Memory Leak Log Specifications.

[h2]Ion Kernel Control

Overview: When the application ion memory leak exceeds 1/3 of the running memory of the entire system and the entire system is in the low memory state, the ion kernel control is triggered.

Log file: memleak-kernel-[module]-0-[timestamp].txt (method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (method 3)

The content of the ion kernel control log is the same as the maintenance and debugging content under the LOGGER_MEMCHECK_PROC_INFO field in ion Memory Leak Log Specifications.

[h2]Thread Kernel Control

Overview: If too many threads are applied for and the scheduling of the entire system is insufficient, thread kernel control is triggered.

Log file: threadleak-#{module}-#{pid}-log-#{timestamp}.txt (method 1) or RESOURCE_OVERLIMIT_[TIMESTAMP]_[PID].log (method 3)

The content of the thread kernel control log is as follows:

Field name. Description
pidNumber PID of the leaked app.
processName Leaked application process name.
killTime Time when an application is killed.
foreGround Application foreground and background status. The options are yes and no.
killReason Reason why the app is killed.
threadCount Total number of threads created by the app.
Generated by HiviewDFX @HarmonyOS
LOGGER_MEMCHECK_GERNAL_INFO
 pidNumber : 8387
 processName : process1
 killTime : 20260512090030
 foreGround : no
 killReason : ThreadKiller
 threadCount : 3001

the process is killed by runnable thread threshold 3001 / 4124.
process1:2099
Thread_HI:9
Thread_GI:9
Thread_FI:9
Thread_DI:9
Thread_BI:9
Thread_EI:9
Thread_AI:9
Thread_CI:9
......

Description

  1. Logs show the top 10 number of threads, such as "process1" and "Thread_HI," after clustering by thread name. process1:2099 indicates that the number of threads process1 is 2099.

  2. In the preceding information, "the process is killed by runnable thread threshold 3001 / 4124." indicates that the total number of threads is 4124 and the total number of threads in the running and runnable states is 3001.