• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Memory Leak Check
2
3## Basic Concepts
4
5As an optional function of the kernel, memory leak check is used to locate dynamic memory leak problems. After this function is enabled, the dynamic memory automatically records the link registers \(LRs\) used when memory is allocated. If a memory leak occurs, the recorded information helps locate the memory allocated for further analysis.
6
7## Function Configuration
8
91.  **LOSCFG\_MEM\_LEAKCHECK**: specifies the setting of the memory leak check. This function is disabled by default. To enable the function, set this macro to  **1**  in  **target\_config.h**.
102.  **LOSCFG\_MEM\_RECORD\_LR\_CNT**: number of LRs recorded. The default value is  **3**. Each LR consumes the memory of  **sizeof\(void \*\)**  bytes.
113.  **LOSCFG\_MEM\_OMIT\_LR\_CNT**: number of ignored LRs. The default value is  **4**, which indicates that LRs are recorded from the time when  **LOS\_MemAlloc**  is called. You can change the value based on actual requirements. This macro is configured because:
12    -   **LOS\_MemAlloc**  is also called internally.
13    -   **LOS\_MemAlloc**  may be encapsulated externally.
14    -   The number of LRs configured by  **LOSCFG\_MEM\_RECORD\_LR\_CNT**  is limited.
15
16
17Correctly setting this macro can ignore invalid LRs and reduce memory consumption.
18
19## Development Guidelines
20
21### How to Develop
22
23Memory leak check provides a method to check for memory leak in key code logic. If this function is enabled, LR information is recorded each time when memory is allocated. When  **LOS\_MemUsedNodeShow**  is called before and after the code snippet is checked, information about all nodes that have been used in the specified memory pool is printed. You can compare the node information. The newly added node information indicates the node where the memory leak may occur. You can locate the code based on the LR and further check whether a memory leak occurs.
24
25The node information output by calling  **LOS\_MemUsedNodeShow**  is in the following format: Each line contains information about a node. The first column indicates the node address, based on which you can obtain complete node information using a tool such as a GNU Debugger \(GDB\). The second column indicates the node size, which is equal to the node header size plus the data field size. Columns 3 to 5 list the LR addresses. You can determine the specific memory location of the node based on the LR addresses and the assembly file.
26
27```
28node        size   LR[0]      LR[1]       LR[2]
290x10017320: 0x528 0x9b004eba  0x9b004f60  0x9b005002
300x10017848: 0xe0  0x9b02c24e  0x9b02c246  0x9b008ef0
310x10017928: 0x50  0x9b008ed0  0x9b068902  0x9b0687c4
320x10017978: 0x24  0x9b008ed0  0x9b068924  0x9b0687c4
330x1001799c: 0x30  0x9b02c24e  0x9b02c246  0x9b008ef0
340x100179cc: 0x5c  0x9b02c24e  0x9b02c246  0x9b008ef0
35```
36
37>![](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
38>Enabling memory leak check affects memory application performance. LR addresses will be recorded for each memory node, increasing memory overhead.
39
40### Development Example
41
42This example implements the following:
43
441.  Call  **LOS\_MemUsedNodeShow**  to print information about all nodes.
452.  Simulate a memory leak by requesting memory without releasing it.
463.  Call  **LOS\_MemUsedNodeShow**  to print information about all nodes.
474.  Compare the logs to obtain information about the node where a memory leak occurred.
485.  Locate the code based on the LR address.
49
50### Sample Code
51
52The sample code is as follows:
53
54```
55#include <stdio.h>
56#include <string.h>
57#include "los_memory.h"
58#include "los_config.h"
59
60void MemLeakTest(void)
61{
62    LOS_MemUsedNodeShow(LOSCFG_SYS_HEAP_ADDR);
63    void *ptr1 = LOS_MemAlloc(LOSCFG_SYS_HEAP_ADDR, 8);
64    void *ptr2 = LOS_MemAlloc(LOSCFG_SYS_HEAP_ADDR, 8);
65    LOS_MemUsedNodeShow(LOSCFG_SYS_HEAP_ADDR);
66}
67```
68
69### Verification
70
71The log is as follows:
72
73```
74node         size   LR[0]       LR[1]       LR[2]
750x20001b04:  0x24   0x08001a10  0x080035ce  0x080028fc
760x20002058:  0x40   0x08002fe8  0x08003626  0x080028fc
770x200022ac:  0x40   0x08000e0c  0x08000e56  0x0800359e
780x20002594:  0x120  0x08000e0c  0x08000e56  0x08000c8a
790x20002aac:  0x56   0x08000e0c  0x08000e56  0x08004220
80
81node         size   LR[0]       LR[1]       LR[2]
820x20001b04:  0x24   0x08001a10  0x080035ce  0x080028fc
830x20002058:  0x40   0x08002fe8  0x08003626  0x080028fc
840x200022ac:  0x40   0x08000e0c  0x08000e56  0x0800359e
850x20002594:  0x120  0x08000e0c  0x08000e56  0x08000c8a
860x20002aac:  0x56   0x08000e0c  0x08000e56  0x08004220
870x20003ac4:  0x1d   0x08001458  0x080014e0  0x080041e6
880x20003ae0:  0x1d   0x080041ee  0x08000cc2  0x00000000
89```
90
91The difference between the two logs is as follows. The following memory nodes are suspected to have blocks with a memory leak.
92
93```
940x20003ac4:  0x1d   0x08001458  0x080014e0  0x080041e6
950x20003ae0:  0x1d   0x080041ee  0x08000cc2  0x00000000
96```
97
98The following is part of the assembly file:
99
100```
101                MemLeakTest:
102  0x80041d4: 0xb510         PUSH     {R4, LR}
103  0x80041d6: 0x4ca8         LDR.N    R4, [PC, #0x2a0]       ; g_memStart
104  0x80041d8: 0x0020         MOVS     R0, R4
105  0x80041da: 0xf7fd 0xf93e  BL       LOS_MemUsedNodeShow    ; 0x800145a
106  0x80041de: 0x2108         MOVS     R1, #8
107  0x80041e0: 0x0020         MOVS     R0, R4
108  0x80041e2: 0xf7fd 0xfbd9  BL       LOS_MemAlloc           ; 0x8001998
109  0x80041e6: 0x2108         MOVS     R1, #8
110  0x80041e8: 0x0020         MOVS     R0, R4
111  0x80041ea: 0xf7fd 0xfbd5  BL       LOS_MemAlloc           ; 0x8001998
112  0x80041ee: 0x0020         MOVS     R0, R4
113  0x80041f0: 0xf7fd 0xf933  BL       LOS_MemUsedNodeShow    ; 0x800145a
114  0x80041f4: 0xbd10         POP      {R4, PC}
115  0x80041f6: 0x0000         MOVS     R0, R0
116```
117
118The memory node addressed by  **0x080041ee**  is not released after being requested in  **MemLeakTest**.
119
120