1# Memory Information Statistics<a name="EN-US_TOPIC_0000001078716768"></a> 2 3- [Basic Concepts](#section52691565235) 4- [Function Configuration](#section470611682411) 5- [Development Guidelines](#section9368374243) 6 - [How to Develop](#section679912407257) 7 - [Development Example](#section1025453412611) 8 9 10## Basic Concepts<a name="section52691565235"></a> 11 12Memory information includes the memory pool size, memory usage, remaining memory size, maximum free memory, memory waterline, number of memory nodes, and fragmentation rate. 13 14- Memory waterline: indicates the maximum memory used in a memory pool. The waterline value is updated upon each memory allocation and release. The memory pool size can be optimized based on this value. 15 16- Fragmentation rate: indicates the fragmentation degree of the memory pool. If the fragmentation rate is high, there are a large number of free memory blocks in the memory pool but each block is small. You can use the following formula to calculate the fragmentation rate: 17 18 Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size 19 20 21- Other statistics: When **LOS\_MemInfoGet** is called, the node information in the memory pool is scanned and related statistics are collected. 22 23## Function Configuration<a name="section470611682411"></a> 24 25**LOSCFG\_MEM\_WATERLINE**: specifies the setting of the memory information statistics function. This function is disabled by default. To enable this function, configure it in **Debug-\> Enable memory pool waterline or not in the configuration item**. If you want to obtain the memory waterline, you must enable this macro. 26 27## Development Guidelines<a name="section9368374243"></a> 28 29### How to Develop<a name="section679912407257"></a> 30 31Key structure: 32 33``` 34typedef struct { 35 UINT32 totalUsedSize; // Memory usage of the memory pool 36 UINT32 totalFreeSize; // Remaining memory in the memory pool 37 UINT32 maxFreeNodeSize; // Maximum size of the free memory block in the memory pool 38 UINT32 usedNodeNum; // Number of non-free memory blocks in the memory pool 39 UINT32 freeNodeNum; // Number of free memory blocks in the memory pool 40#if (LOSCFG_MEM_WATERLINE == 1) // This function is disabled by default and can be enabled using the menuconfig tool. 41 UINT32 usageWaterLine; // Waterline of the memory pool 42#endif 43} LOS_MEM_POOL_STATUS; 44``` 45 46- To obtain the memory waterline, call **LOS\_MemInfoGet**. The first parameter in the API is the start address of the memory pool, and the second parameter is the handle of the **LOS\_MEM\_POOL\_STATUS** type. The **usageWaterLine** field indicates the waterline. 47 48- To calculate the memory fragmentation rate, call **LOS\_MemInfoGet** to obtain the remaining memory size and the maximum free memory block size in the memory pool, and then calculate the fragmentation rate of the dynamic memory pool as follows: 49 50 Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size 51 52 53### Development Example<a name="section1025453412611"></a> 54 55This example implements the following: 56 571. Creates a monitoring task to obtain information about the memory pool. 582. Calls **LOS\_MemInfoGet** to obtain the basic information about the memory pool. 593. Calculates the memory usage and fragmentation rate. 60 61**Sample Code** 62 63The sample code is as follows: 64 65``` 66#include <stdio.h> 67#include <string.h> 68#include "los_task.h" 69#include "los_memory.h" 70#include "los_config.h" 71 72void MemInfoTaskFunc(void) 73{ 74 LOS_MEM_POOL_STATUS poolStatus = {0}; 75 76 /* pool is the memory address of the information to be collected. OS_SYS_MEM_ADDR is used as an example.*/ 77 void *pool = OS_SYS_MEM_ADDR; 78 LOS_MemInfoGet(pool, &poolStatus); 79 /* Calculate the fragmentation rate of the memory pool. */ 80 unsigned char fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize; 81 /* Calculate the memory usage of the memory pool. */ 82 unsigned char usage = LOS_MemTotalUsedGet(pool) * 100 / LOS_MemPoolSizeGet(pool); 83 printf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment, poolStatus.maxFreeNodeSize, 84 poolStatus.totalFreeSize, poolStatus.usageWaterLine); 85} 86 87int MemTest(void) 88{ 89 unsigned int ret; 90 unsigned int taskID; 91 TSK_INIT_PARAM_S taskStatus = {0}; 92 taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc; 93 taskStatus.uwStackSize = 0x1000; 94 taskStatus.pcName = "memInfo"; 95 taskStatus.usTaskPrio = 10; 96 ret = LOS_TaskCreate(&taskID, &taskStatus); 97 if (ret != LOS_OK) { 98 printf("task create failed\n"); 99 return -1; 100 } 101 return 0; 102} 103``` 104 105**Verification** 106 107The result is as follows: 108 109``` 110usage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414 111``` 112 113