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

查看:260
本文介绍了如何可视化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 )。所有 malloc ing发生在启动时,所有免费 ing发生在应用程序的末尾(实际上从来没有应用程序的主要部分是无限循环)。所以我确信碎片不是问题。

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:你有一个16 K堆,已经爆发了10K,你尝试和m​​alloc另外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:更常见的是,你有一个16 k堆,你一直在做一堆malloc / free / realloc调用和你的堆小于50%'full':您将malloc称为1K,它失败了 - 什么?答案 - 堆可用空间是碎片 - 没有可以返回的1K可用内存。当这种情况发生时,堆堆经理不能压缩堆,所以你一般都是坏的。有一些技术可以避免碎片化,但很难知道这是否真的成为问题。您需要添加日志垫片来进行malloc和免费,以便您可以了解正在执行的动态内存操作。

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];



< 。不要忘记减小堆大小 - 但请注意,某些运行时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天全站免登陆