1# Memory Information Statistics<a name="EN-US_TOPIC_0000001078916690"></a> 2 3- [Basic Concepts](#section52691565235) 4- [Function Configuration](#section470611682411) 5- [Development Guidelines](#section9368374243) 6 - [How to Develop](#section679912407257) 7 - [Development Example](#section1025453412611) 8 - [Sample Code](#section165277971315) 9 - [Verification](#section3460102414271) 10 11 12## Basic Concepts<a name="section52691565235"></a> 13 14Memory information includes the memory pool size, memory usage, remaining memory size, maximum free memory, memory waterline, number of memory nodes, and fragmentation rate. 15 16- 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. 17 18- 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: 19 20 Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size 21 22 23- Other parameters: You can call APIs \(described in [Memory Management](kernel-mini-basic-memory-basic.md)\) to scan node information in the memory pool and collect statistics. 24 25## Function Configuration<a name="section470611682411"></a> 26 27**LOSCFG\_MEM\_WATERLINE**: specifies the setting of the memory information statistics function. This function is enabled by default. To disable the function, set this macro to **0** in **target\_config.h**. If you want to obtain the memory waterline, you must enable this macro. 28 29## Development Guidelines<a name="section9368374243"></a> 30 31### How to Develop<a name="section679912407257"></a> 32 33Key structure: 34 35``` 36typedef struct { 37 UINT32 totalUsedSize; // Memory usage of the memory pool 38 UINT32 totalFreeSize; // Remaining memory in the memory pool 39 UINT32 maxFreeNodeSize; // Maximum size of the free memory block in the memory pool 40 UINT32 usedNodeNum; // Number of non-free memory blocks in the memory pool 41 UINT32 freeNodeNum; // Number of free memory blocks in the memory pool 42#if (LOSCFG_MEM_WATERLINE == 1) // This macro is enabled by default. To disable it, set it to 0 in target_config.h. 43 UINT32 usageWaterLine; // Waterline of the memory pool 44#endif 45} LOS_MEM_POOL_STATUS; 46``` 47 48- 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. 49 50- 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: 51 52 Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size 53 54 55### Development Example<a name="section1025453412611"></a> 56 57This example implements the following: 58 591. Creates a monitoring task to obtain information about the memory pool. 60 612. Calls **LOS\_MemInfoGet** to obtain the basic information about the memory pool. 62 633. Calculates the memory usage and fragmentation rate. 64 65### Sample Code<a name="section165277971315"></a> 66 67The sample code is as follows: 68 69``` 70#include <stdio.h> 71#include <string.h> 72#include "los_task.h" 73#include "los_memory.h" 74#include "los_config.h" 75 76void MemInfoTaskFunc(void) 77{ 78 LOS_MEM_POOL_STATUS poolStatus = {0}; 79 80 /* pool is the memory address of the information to be collected. OS_SYS_MEM_ADDR is used as an example.*/ 81 void *pool = OS_SYS_MEM_ADDR; 82 LOS_MemInfoGet(pool, &poolStatus); 83 /* Calculate the fragmentation rate of the memory pool. */ 84 unsigned char fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize; 85 /* Calculate the memory usage of the memory pool. */ 86 unsigned char usage = LOS_MemTotalUsedGet(pool) * 100 / LOS_MemPoolSizeGet(pool); 87 printf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment, poolStatus.maxFreeNodeSize, 88 poolStatus.totalFreeSize, poolStatus.usageWaterLine); 89} 90 91int MemTest(void) 92{ 93 unsigned int ret; 94 unsigned int taskID; 95 TSK_INIT_PARAM_S taskStatus = {0}; 96 taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc; 97 taskStatus.uwStackSize = 0x1000; 98 taskStatus.pcName = "memInfo"; 99 taskStatus.usTaskPrio = 10; 100 ret = LOS_TaskCreate(&taskID, &taskStatus); 101 if (ret != LOS_OK) { 102 printf("task create failed\n"); 103 return -1; 104 } 105 return 0; 106} 107``` 108 109### Verification<a name="section3460102414271"></a> 110 111The result is as follows: 112 113``` 114usage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414 115``` 116 117