get RAM usage - free stack space

Post your cool example code here.
Post Reply
ag123
Posts: 742
Joined: Thu Jul 21, 2016 4:24 pm

get RAM usage - free stack space

Post by ag123 » Mon May 08, 2017 5:41 am

found some rather 'obscure' codes that determine the free stack space from greiman stfat's repository
https://github.com/greiman/SdFat-beta/b ... tack.h#L45

Code: Select all

#if defined(__arm__)
extern "C" char* sbrk(int incr);
static int FreeStack() {
  char top = 't';
  return &top - reinterpret_cast<char*>(sbrk(0));
}
#endif
imho this is a nice hack, sbrk is normally at the 'bottom' where malloc allocates memory and moves up, hence the code allocate a variable on the stack (called it top) and measure the distance between 'top' to sbrk();

victor_pv
Posts: 1654
Joined: Mon Apr 27, 2015 12:12 pm

Re: get RAM usage - free stack space

Post by victor_pv » Mon May 08, 2017 5:27 pm

Nice find. :)

Post Reply