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

文章目录

    • [RT-Thread 堆内存 检查命令 free 实现及介绍](#RT-Thread 堆内存 检查命令 free 实现及介绍)
      • [rt_memory_info 函数验证](#rt_memory_info 函数验证)

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

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

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 函数,可以使用这个函数来查看当前堆的使用情况:

c 复制代码
#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 命令即可查看堆的使用情况:

shell 复制代码
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函数获取的信息是否正确。

c 复制代码
#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不能导致内存泄露。

相关推荐
森G1 天前
五、Linux字符设备驱动
linux·arm开发·c++·ubuntu
贝塔实验室1 天前
新手如何使用Altium Designer创建第一张原理图(三)
arm开发·单片机·嵌入式硬件·fpga开发·射频工程·基带工程·嵌入式实时数据库
小尧嵌入式1 天前
QT软件开发知识点流程及图片转换工具的开发
开发语言·arm开发·qt
贝塔实验室1 天前
Altium Designer全局编辑
arm开发·经验分享·笔记·fpga开发·dsp开发·射频工程·基带工程
dian2008-ic1 天前
Linux pcie【9】基于GIC-V3 ITS实现pcie msi中断
linux·arm开发·驱动开发·嵌入式硬件
大大菜鸟一枚1 天前
ARM交叉编译环境配置与Qt依赖库部署指南
开发语言·arm开发·qt
CinzWS1 天前
TrustZone-M的设计哲学
arm开发·arm·architecture
macken99991 天前
centos7arm版本镜像下载存档
arm开发·x86_64开发
贝塔实验室2 天前
新手如何使用Altium Designer创建第一张原理图(二)
arm开发·fpga开发·硬件工程·dsp开发·射频工程·基带工程·嵌入式实时数据库
ShiMetaPi2 天前
GM-3568JHF丨ARM+FPGA异构开发板系列教程:基础入门 07 测试命令
arm开发·fpga开发