如何可视化 AVR 程序的内存 (SRAM) 使用情况? [英] How can I visualise the memory (SRAM) usage of an AVR program?

查看:21
本文介绍了如何可视化 AVR 程序的内存 (SRAM) 使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AVR 微控制器 (ATMega328P) 上运行的 C 程序中遇到了问题.我相信这是由于堆栈/堆冲突,但我希望能够确认这一点.

I have encountered a problem in a C program running on an AVR microcontroller (ATMega328P). I believe it is due to a stack/heap collision but I'd like to be able to confirm this.

有什么方法可以可视化堆栈和堆的 SRAM 使用情况?

Is there any way I can visualise SRAM usage by the stack and the heap?

注意:程序用avr-gcc编译,使用avr-libc.

Note: the program is compiled with avr-gcc and uses avr-libc.

更新: 我遇到的实际问题是 malloc 实现失败(返回 NULL).所有 mallocing 都发生在启动时,所有 freeing 都发生在应用程序结束时(实际上从来没有,因为应用程序的主要部分处于无限循环中).所以我确信碎片化不是问题.

Update: The actual problem I am having is that the malloc implementation is failing (returning NULL). All mallocing happens on startup and all freeing happens at the end of the application (which in practice is never since the main part of the application is in an infinite loop). So I'm sure fragmentation is not the issue.

推荐答案

你说 malloc 失败并返回 NULL:

You say malloc is failing and returning NULL:

您应该首先查看的明显原因是您的堆已满" - 即您要求 malloc 的内存无法分配,因为它不可用.

The obvious cause which you should look at first is that your heap is "full" - i.e, the memory you've asked to malloc cannot be allocated, because it's not available.

要记住两种情况:

a:你有一个 16K 的堆,你已经 malloc 了 10K,你尝试再 malloc 10K.你的堆太小了.

a: You have a 16 K heap, you've already malloced 10 K and you try and malloc a further 10K. Your heap is simply too small.

b:更常见的是,你有一个 16k 的堆,你一直在做一堆 malloc/free/realloc 调用,你的堆不到 50% '满':你调用 malloc 来获取 1K 并且它失败 -这是怎么回事?答案 - 堆可用空间是碎片化的 - 没有可以返回的连续 1K 可用内存.发生这种情况时,C 堆管理器无法压缩堆,因此您通常情况不佳.有一些技术可以避免碎片化,但很难知道这是否真的是问题所在.您需要向 malloc 和 free 添加日志填充程序,以便了解正在执行的动态内存操作.

b: More commonly, you have a 16 k Heap, you've been doing a bunch of malloc/free/realloc calls and your heap is less than 50% 'full': You call malloc for 1K and it FAILS - what's up? Answer - the heap free space is fragmented - there isn't a contigous 1K of free memory that can be returned. C Heap managers can not compact the heap when this happens, so you're generally in a bad way. There are techniques to avoid fragmentation, but it's difficult to know if this is really the problem. You'd need to add logging shims to malloc and free so that you can get an idea of what dynamic memory operations are being performed.

你说所有 malloc 都发生在启动时,所以碎片不是问题.

You say all mallocs happen at startup, so fragmentation isn't the issue.

在这种情况下,用静态替换动态分配应该很容易.

In which case, it should be easy to replace the dynamic allocation with static.

旧代码示例:

char *buffer;

void init()
{
  buffer = malloc(BUFFSIZE);
}

新代码:

char buffer[BUFFSIZE];

一旦你在所有地方都这样做了,你的 LINKER 应该会在所有内容都无法放入可用内存时发出警告.不要忘记减小堆大小 - 但要注意一些运行时 io 系统函数可能仍然使用堆,因此您可能无法完全删除它.

Once you've done this everywhere, your LINKER should warn you if everything cannot fit into the memory available. Don't forget to reduce the heap size - but beware that some runtime io system functions may still use the heap, so you may not be able to remove it entirely.

这篇关于如何可视化 AVR 程序的内存 (SRAM) 使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆