RT-Thread Getting Started Study Notes-Use of menuconfig Kconfig
RT-Thread Getting Started Study Notes-Familiar with dynamic memory application and release
RT-Thread Getting Started Study Notes-View the address of the thread stack
RT-Thread Getting Started Study Notes-Solve the problem of RT_ASSERT failure
Preface
- Recently used RT-Thread's dynamic memory management: rt_malloc rt_free, summarize the experience.
- Master the application and release of dynamic memory.
Problem Description
- After using rt_malloc for one byte, it was found that the memory consumption: 24 bytes!
- After using rt_free a dynamically applied buf, the buf pointer is still there!
problem analysis
Verification platform: Pandora STM32L4 platform, RT-Thread 4.0.3
Test function:
rt_uint8_t *ptr_buf = RT_NULL;
void mem_malloc_test(int argc, char **argv)
{
rt_uint32_t buf_len = 0x00;
if (argc >= 2)
{
buf_len = atoi(argv[1]);
ptr_buf = rt_malloc(buf_len);
rt_kprintf("%s:malloc len=%d\n", __func__, buf_len);
rt_kprintf("%s: ptr=%p\n", __func__, ptr_buf);
}
else
{
rt_kprintf("help: mem_malloc_test 3\n");
}
}
void mem_free_test(void)
{
if (ptr_buf != RT_NULL)
{
rt_free(ptr_buf);
rt_kprintf("%s: free OK!\n", __func__);
rt_kprintf("%s: ptr=%p\n", __func__, ptr_buf);
}
else
{
rt_kprintf("%s: error! buffer is null!\n", __func__);
}
}
MSH_CMD_EXPORT(mem_malloc_test, memory malloc test);
MSH_CMD_EXPORT(mem_free_test, memory free test);
The following table:
init | rt_malloc | rt_free | buf_len | total size | diff |
---|---|---|---|---|---|
28788 | 28812 | 28788 | 1 | 24 | 23 |
28788 | 28812 | 28788 | 4 | 24 | 20 |
28788 | 28812 | 28788 | 8 | 24 | 16 |
28788 | 28812 | 28788 | 10 | 24 | 14 |
28788 | 28812 | 28788 | 11 | 24 | 13 |
28788 | 28812 | 28788 | 12 | 24 | 12 |
28788 | 28816 | 28788 | 13 | 28 | 15 |
28788 | 28816 | 28788 | 16 | 28 | 12 |
28788 | 28820 | 28788 | 20 | 32 | 12 |
28788 | 28824 | 28788 | 24 | 36 | 12 |
28788 | 28900 | 28788 | 100 | 112 | 12 |
28788 | 39040 | 28788 | 10240 | 10252 | 12 |
28788 | 69760 | 28788 | 40960 | 40972 | 12 |
- Apply for 1~12 bytes, all will occupy: 24 bytes.
- The number of bytes requested is not 4-byte aligned, but still occupies the same size as 4-byte aligned. If you apply for 13~16 bytes, the occupied memory is 28.
- When applying for more than 12 bytes of memory, such as 13, 16, 100, 1K, 10K, 40K, etc., the additional occupancy of the memory management part is 12 bytes.
- rt_malloc and rt_free are used in pairs, which will not cause memory leakage
- After rt_free, the free buffer is gone, but the buffer pointer value does not change!
Key issues
- After applying for memory, the buf and buf pointers of rt_free still exist. So after rt_free, you need to manually assign buf to null.
- After rt_free, the buf pointer is still there. If you don't manually assign the buf pointer to null and free the buf many times, unexpected results will occur!
- Below, I use a global pointer, after applying for memory, free multiple times (after free, the pointer is not manually changed to null)
- Remarks: After verification, it was found that the byte accidentally turned off the RT_ASSERT function, otherwise, if rt_free is repeated, an ASSERT assertion will appear! !
- The following data is for reference only, meaning: after rt_free, you need to manually change the pointer to null, and you cannot repeat free!
msh />free
total memory: 82256
used memory : 24564
maximum allocated memory: 27572
msh />mem_mal
mem_malloc_test
msh />mem_malloc_test 12
mem_malloc_test:malloc len=12
mem_malloc_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 24588
maximum allocated memory: 27572
msh />mem_free_test
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28788
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28764 /* 内存在变小!!!! */
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28740 /* 内存在变小!!!! */
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28692
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28668 /* 内存在变小!!!! */
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28644 /* 内存在变小!!!! */
maximum allocated memory: 29604
msh />mem_free_test
to free a bad data block:
mem: 0x20003e98, used flag: 0, magic code: 0x1ea0
mem_free_test: free OK!
mem_free_test: ptr=20003ea4
msh />free
total memory: 82256
used memory : 28620 /* 内存在变小!!!! */
maximum allocated memory: 29604
msh />
- A global pointer was found. After applying for memory once, it was free for many times. Although an error was reported, it was still free! !
to sum up
- Use rt_malloc and rt_free correctly, appear in pairs! !
- Although static memory is easy to use, it always occupies memory. Therefore, for some variable-length data receiving buffers, dynamic memory management can be used.
- Note that the memory after free, the pointer address is still there, if it is a global pointer, it is best to manually change to null after free.