Oversized stack memory of an app is one of the level-2 root causes of RSS memory leaks. This topic describes the possible root causes of oversized stack memory and provides case studies to help you analyze problems in the development and O&M states.
As the basic memory area for program running, the stack memory is typically used to store information such as local variables, function call chains, and return addresses. Generally, the stack memory is automatically managed by the system and is automatically released when a function returns. It is believed in the industry that stack memory does not leak. However, in the actual development and O&M of HarmonyOS, oversized stack memory can also lead to severe memory leak issues.
Memory leak caused by oversized stack memory is rooted in unbalanced thread lifecycle management, which is typically manifested in two types of issues:
You can analyze the lightweight smaps maintenance and debugging logs based on the basic log analysis method. If the memory usage of the stack category is the largest, you can narrow down the RSS memory leak issue to a stack memory leak issue. To further analyze the leak point, you can obtain the memory stack logs by referring to the method of obtaining memory stack logs, import the logs to DevEco Studio, and analyze the call stack based on the memory stack log analysis method to locate the leak point.
The speed of memory leaks has a great impact on the validity of the application memory stack. If the speed is too fast or too slow, the memory call stack may fail to hit the leak point. For fast memory leaks, you are advised to subscribe to resource leak events by referring to Subscribing to Resource Leak Events (ArkTS). In addition, use the configEventPolicy() method to subscribe to app page switching information to analyze the leak scenario and reproduce and locate the leak issue in the development state.
If an RSS memory leak occurs in your app, you can reproduce the issue based on the scenario and use the Allocation feature of the Profiler tool in DevEco Studio to locate the stack memory leak issue by referring to Analysis approach in the development state.
To clearly demonstrate how to locate memory leaks caused by oversized stack memory, the following describes the analysis process in the development and O&M states based on a negative case where memory leaks occur due to excessive thread requests.
In this negative case, the app requests a large number of threads but does not release them, resulting in excessive stack memory usage. After detecting an RSS memory leak in the app, the system manages and controls the app in the foreground. As a result, the app crashes.
Analysis approach in the O&M state
If you encounter this issue in the O&M state, perform the following steps for analysis:
LOGGER_MEMCHECK_SAMPLIFY_SMAPS_INFO
Size Rss Swap Category Counts Name
512 0 52 arkts-static heap 2 [anon:ArkTs Static Object Space]
1028 12 0 stack 1 [anon:stack:55575]
1028 12 0 stack 1 [anon:stack:55576]
1028 12 0 stack 1 [anon:stack:55577]
1028 12 0 stack 1 [anon:stack:55578]
...... 

Analysis approach in the development state
For problems in the development state, you can roughly infer the scenario where the RSS memory leak occurs. You can perform the following steps to check whether the problem is caused by thread stack leak and locate the fault:
# hidumper --mem-smaps 43709
-------------------------------[memory]-------------------------------
Shared Shared Private Private
Size Rss Pss Clean Dirty Clean Dirty Swap SwapPss Counts Category Name
......
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43766]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43767]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43768]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43770]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43802]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43817]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:43878]
28 0 0 0 0 0 0 0 0 1 stack [anon:signal_stack:654]
......
1028 12 12 0 0 12 0 0 0 1 stack [anon:stack:43766]
1028 12 12 0 0 12 0 0 0 1 stack [anon:stack:43767]
1028 16 16 0 0 16 0 0 0 1 stack [anon:stack:43768]
8196 24 24 0 0 24 0 0 0 1 stack [anon:stack:43816]
1028 28 28 0 0 28 0 0 0 1 stack [anon:stack:43817]
1028 8 8 0 0 8 0 0 0 1 stack [anon:stack:43878]
...... 
