Teensy 3.2 RAM usage tools?

Status
Not open for further replies.

jim lee

Well-known member
Is there any tools to help debugging dynamic RAM usage? For example, to look at what has been allocated and what is free? I'm looking for something to see how much RAM is being used by different parts of my code and if things are "leaking" RAM. Even if I have to roll my own, I don't know where/how to access this kind of information.

Thanks!

-jim lee
 
I have always sort of rolled my own...

For quick and dirty, I simply check to see if it looks like the whole heap area is growing or not.

I will sometimes put into sketches something like:
Code:
extern "C" void * _sbrk(int incr);
...
  Serial.printf("Estimated Heap usage: %u\n", (uint32_t)(_sbrk(0)) - (uint32_t)&_heap_start);

This I think came from one of Defragster's sketches.

Sometimes I do it like this, other times I simply
use the value of _sbrk(0);
from previous areas and see how much it has grown since the last time... (decreased)

Note: _sbrk is used by the core code when you call things like malloc or new and it needs to grow the heap to accommodate the request

But what this does not do, is walk through the heap to see which parts have been freed and which ones are still allocated. I used to do this a long time ago, with real simple memory allocation system....

Good luck
 
I found this in malloc.c
Code:
extern char __heap_start;
extern char __heap_end;
#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)

struct __freelist {
	size_t sz;
	struct __freelist *nx;
};

/*
 * Exported interface:
 *
 * When extending the data segment, the allocator will not try to go
 * beyond the current stack limit, decreased by __malloc_margin bytes.
 * Thus, all possible stack frames of interrupt routines that could
 * interrupt the current function, plus all further nested function
 * calls must not require more stack space, or they'll risk to collide
 * with the data segment.
 */
 
size_t __malloc_margin = 128;
char *__malloc_heap_start = &__heap_start;
char *__malloc_heap_end = &__heap_end;

Is there a malloc.h somewhere I can include to access this?
For example : where are __heap_start & __heap_end defined?
I can't figure this out.

Wait no.. Some of this is beginning to make a ittle sense..

-jim lee
 
Last edited:
Great, There are other samples and snippets around - that was the first I found with a post's link comment in a local sample - and it happened to be T_3.2 era ...
 
Status
Not open for further replies.
Back
Top