Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Teensy 3.2 RAM usage tools?

  1. #1
    Senior Member jim lee's Avatar
    Join Date
    Dec 2016
    Location
    Anacortes, WA
    Posts
    180

    Teensy 3.2 RAM usage tools?

    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

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,042
    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

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,553
    Found this example of one posted : Memory-Status-and-Monitoring

  4. #4
    Senior Member jim lee's Avatar
    Join Date
    Dec 2016
    Location
    Anacortes, WA
    Posts
    180
    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 by jim lee; 11-15-2021 at 02:04 AM. Reason: Added even more confusion.

  5. #5
    Senior Member jim lee's Avatar
    Join Date
    Dec 2016
    Location
    Anacortes, WA
    Posts
    180
    Quote Originally Posted by defragster View Post
    Found this example of one posted : Memory-Status-and-Monitoring
    I found a copy of this on github and grabbed a copy of that. Wrapping it in a c++ class that works in my app framework, I ended up with the slickest little memory montor.

    Thanks guys!

    -jim llee

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    15,553
    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 ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •