【ARMv8M Cortex-M33 系列 8.1 -- RT-Thread 堆内存 检查命令 free 实现及介绍】

本文介绍了在RT-Thread系统中如何使用rt_memory_info函数获取堆内存信息,并展示了如何通过cmd_free命令查看堆内存使用情况。同时提供了一个测试函数来验证rt_memory_info的准确性,强调了测试用例中避免内存泄露的重要性。
摘要由CSDN通过智能技术生成


请阅读【嵌入式开发学习必备专栏 】


RT-Thread 堆内存 检查命令 free 实现及介绍

在RT-Thread系统中,通常可以通过rt_memory_info函数获取当前的堆内存使用信息,然后你可以包装这个函数来显示剩余的堆空间。rt_memory_info实现见:
rt-thread/src/mem.c:

void rt_memory_info(rt_uint32_t *total,
                    rt_uint32_t *used,
                    rt_uint32_t *max_used)
{
    if (total != RT_NULL)
        *total = mem_size_aligned;
    if (used  != RT_NULL)
        *used = used_mem;
    if (max_used != RT_NULL)
        *max_used = max_mem;
}

rt-thread 中其实已经实现了cmd_free 函数,可以使用这个函数来查看当前堆的使用情况:

#ifdef RT_USING_HEAP
int cmd_free(int argc, char **argv)
{
    rt_uint32_t total = 0, used = 0, max_used = 0;

    rt_memory_info(&total, &used, &max_used);
    rt_kprintf("total   : %d\n", total);
    rt_kprintf("used    : %d\n", used);
    rt_kprintf("maximum : %d\n", max_used);
    return 0;
}
MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system.);
#endif /* RT_USING_HEAP */

所以在终端执行free 命令即可查看堆的使用情况:

msh >help
RT-Thread shell commands:
list             - list all commands in system
list_timer       - list timer in system
list_mempool     - list memory pool in system
list_memheap     - list memory heap in system
list_msgqueue    - list message queue in system
list_mailbox     - list mail box in system
list_mutex       - list mutex in system
list_event       - list event in system
list_sem         - list semaphore in system
list_thread      - list thread
version          - show RT - Thread version information
clear            - clear the terminal screen
hello            - say hello world
free             - Show the memory usage in the system.
ps               - List threads in the system.
help             - RT - Thread shell help.

rt_memory_info 函数验证

如下实现了一个测试函数,在函数开始的时候查看当前堆使用了多少,然后再进行rt_malloc(1024) 之后再查看下堆使用了多少,通过前后对比可以看出rt_memory_info函数获取的信息是否正确。

#include <rtthread.h>
#include <pthread.h>

#define TEST_MALLOC_SIZE        1024

static int mem_check_test(void)
{
        char *ptr = RT_NULL;
        rt_uint32_t total = 0, used_pre = 0, max_used = 0;
        rt_uint32_t used_next = 0;

        rt_memory_info(&total, &used_pre, &max_used);

        ptr = (char *)rt_malloc(TEST_MALLOC_SIZE);
        if (ptr == RT_NULL) {
                rt_kprintf("mem check test failed\n");
                return -RT_ENOMEM;
        }

        rt_memory_info(&total, &used_next, &max_used);

        if ((used_next - used_pre) != TEST_MALLOC_SIZE + 16) {
                rt_kprintf("mem check test failed\n"
                           "mem used_pre: %d, mem used_next:%d\n",
                           used_pre, used_next);

                rt_free(ptr);

                return -RT_ERROR;
        }

        rt_kprintf("mem check test ok\n");

        rt_free(ptr);

	return RT_EOK;
}
INIT_APP_EXPORT(mem_check_test);

关于free命令的本地测试如下:
在这里插入图片描述
通过执行free命令之后可以看到一共有多少heap和已经使用了多少。

通常需要在跑完测试用例后不能影响heap的大小,简单点说就是你的测试case不能导致内存泄露。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

主公CodingCos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值