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

Thread: How to display free ram.

  1. #1
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    273

    How to display free ram.

    With my arduino nano I'm using the following function to display free ram.
    Code:
    int freeRam ()
    {
      extern int __heap_start, *__brkval;
      int v;
      return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
    }
    I don't know what/how that code works but would like an equivalent for teensy 3.2.
    Does anyone know how?
    Thanks.

  2. #2
    Senior Member blackketter's Avatar
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    340
    This seems to work for me:

    Code:
    uint32_t FreeMem(){ // for Teensy 3.0
        uint32_t stackTop;
        uint32_t heapTop;
    
        // current position of the stack.
        stackTop = (uint32_t) &stackTop;
    
        // current position of heap.
        void* hTop = malloc(1);
        heapTop = (uint32_t) hTop;
        free(hTop);
    
        // The difference is (approximately) the free, available ram.
        return stackTop - heapTop;
    }

  3. #3
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    273
    That worked fine.
    Thanks.

  4. #4
    Senior Member
    Join Date
    Jan 2015
    Location
    SF Bay Area
    Posts
    255
    sdfat library use this for ARM processor

    Code:
    extern "C" char* sbrk(int incr);
    int SdFatUtil::FreeRam() {
      char top;
      return &top - reinterpret_cast<char*>(sbrk(0));
    }
    if you have sdfat library, and #include <SdFatUtil.h>
    you can just call FreeRam();

  5. #5
    Senior Member
    Join Date
    Oct 2015
    Location
    Vermont, USA
    Posts
    288
    Quote Originally Posted by doughboy View Post
    sdfat library use this for ARM processor

    Code:
    extern "C" char* sbrk(int incr);
    int SdFatUtil::FreeRam() {
      char top;
      return &top - reinterpret_cast<char*>(sbrk(0));
    }
    if you have sdfat library, and #include <SdFatUtil.h>
    you can just call FreeRam();
    This is so nice and tight! It doesn't seem to work for Teensy 4, however (I get negative values). Is there any easy mod to get it to work for both T3 and T4?

    Chip

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,115
    Try replacing "reinterpret_cast<char*>(sbrk(0))" with "(char *)&_heap_end".

    EDIT: oh, wait... using "&top" is completely wrong for Teensy 4.x.

    Try this instead:

    Code:
    extern unsigned long _heap_start;
    extern unsigned long _heap_end;
    extern char *__brkval;
    
    int freeram() {
      return (char *)&_heap_end - __brkval;
    }
    
    
    void setup() {
      while (!Serial) ;
      Serial.print("freeram = ");
      Serial.println(freeram());
      volatile char *p = (char *)malloc(152000);
      *p = 0;
      Serial.print("freeram = ");
      Serial.println(freeram());
    }
    
    void loop() {
    }
    Last edited by PaulStoffregen; 03-29-2021 at 11:36 PM.

  7. #7
    Senior Member
    Join Date
    Oct 2015
    Location
    Vermont, USA
    Posts
    288
    Seems to work great!

    Thanks!

  8. #8
    Senior Member
    Join Date
    Apr 2019
    Posts
    144
    Is there a way to see RAM usage during program execution, in the similar way as repeatedly calling AudioProcessorUsage()? Thanks.

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,115
    With the audio library, you can use AudioMemoryUsage() to see how much of the AudioMemory you reserved is actually in use. Details here:

    https://www.pjrc.com/teensy/td_libs_...onnection.html

    Using the more generic ways discussed on this thread won't work for the audio library. The purpose of using AudioMemory() at the beginning of your program is it takes a portion of Teensy's memory to be constantly allocated for the audio library. So if you use AudioMemory(50) to reserve 50 blocks of audio, to these more generic functions all 50 are always consumed, even if the audio library is only using a portion of them. This is one of the many things the audio library does to minimize the chance of non-audio activity glitching the audio. By keeping all of the audio memory allocated (with respect to the rest of the system) the audio library can manage that memory in a way that gives highly deterministic latency. The downside is all of that memory is dedicated to audio and other libraries can't use any of it, even if the audio library isn't making any use of some portion.

    With regard to the non-audio memory, the code in msg #6 will tell you the portion of RAM2 which is still unused by malloc() & C++ new.



    One way Teensy 4.x differs from earlier Teensy and many other microcontrollers is the local variables are in a physically different memory than the heap for malloc(). A more common arrangement is the heap starts right after the zeroed variable and grows upward in the same memory as the local variables which start at the top and grown downward.

  10. #10
    Senior Member
    Join Date
    Apr 2019
    Posts
    144
    Ok, I'm asking because I tried the code in #6 expecting to see the free RAM size change when called during execution, but it stays the same value. I think I'm misunderstanding something. I was hoping to see if available RAM runs out during execution.
    Last edited by UHF; 04-03-2021 at 02:28 PM.

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,115
    If you run the code from msg #6 and change the number of bytes in the line with malloc(), you should see the reported memory usage change.

  12. #12
    Member
    Join Date
    Apr 2021
    Location
    Cambridgeshire, UK
    Posts
    51
    Am I right in thinking that using __brkval only shows you the "most ever" used heap memory, rather than some estimate of the "current state"? I'm interested in checking for the latter, to ensure (barring fragmentation issues) that a piece of code doesn't have a memory leak. I'm now using:
    Code:
    extern unsigned long _heap_end;
    extern char *__brkval;
    uint32_t FreeMem(){ // for Teensy 4.1
      char* p = (char*) malloc(10000); // size should be quite big, to avoid allocating fragment!
      free(p);
      return (char *)&_heap_end - p; // __brkval;
    }
    The 10000-byte allocation size ensures (in my use case) that p isn't inside the possibly-fragmented start of the heap, giving a misleading idea of the space remaining at the time of the call.

    Cheers

    Jonathan
    Last edited by h4yn0nnym0u5e; 05-05-2021 at 11:25 AM.

Posting Permissions

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