Jump to content

Application RAM Usage

Recommended Posts

I'm trying to keep an eye on the amount of RAM my application is using.  After a bit of searching, I found the following code that returns the current amount of RAM that is being used.  However, the function throws a compiler error about some undefined variables.


Is this the right approach to find current RAM usage?  Or, is there something else I should be using with Energia?






// Return the current amount of RAM used.
// ----------------------------------------------------------------------------
int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);


Flasher2.cpp.o: In function `freeRam()':
C:\DOCUME~1\James\LOCALS~1\Temp\build4403779855877337752.tmp/Flasher2.cpp:178: undefined reference to `__brkval'
C:\DOCUME~1\James\LOCALS~1\Temp\build4403779855877337752.tmp/Flasher2.cpp:178: undefined reference to `__heap_start'
collect2: ld returned 1 exit status

Link to post
Share on other sites

don't know what u got the code fragment from. but the principals are the same.


it is trying to use the address of a variable to determine its position in the heap and thus come up w/ a relative memory usage.


since "int v" is a local variable, i am not sure if it will be allocated in the heap or whether it will be allocated in the stack at run time.


the &v - XXX statement is taking the absolute address of v and minus some starting pointer (__heap_start).


i would imagine you can supply the __heap_start as 0x0200 and try an get your result. i don't understand the __brk_val variable though, may be just assume it's zero for your trial.


if your compiler is able to output a list / memory map file, u can see how rams are used in the heap after code is compiled.


but u still need to worry by the stack at runtime. and u may look at the SP register by inline assembly code.




uint16_t my_sp;
asm("mov r1,%[res]": [res] "=r" (my_sp);

there may be better ways to do this, but the above works for me.

Link to post
Share on other sites

Sorry about not indicating where the snipit came from.  Here is a link to the source article. 


Yeah, I did find the temperary files that Erengia is making after a build is complete.  However, there were no memory map files generated.  Is there a way to adjust a setting to have Erengia generate a map file?


Looks like (according to the link above) "At any point in time, there is a highest point in RAM occupied by the heap. This value can be found in a system variable called __brkval."


Here is another good link - however, it's all about an Arduino.  This guy has shows a couple of diagrams descibing memory layout.  Again, for the Arduino.


I'm guessing the problem is just with the spelling of the variables `__brkval' and `__heap_start'.  Must be some varant of these?


Thanks for the suggestions,


Link to post
Share on other sites

now that makes sense, the "int v" variable actually will happen in the stack, and we use it's address minus the data / bss section last element will give u your free ram size.


example using g2553



0x0400 .... end of ram, also start of stack
0x03xx .... some variable, function call parameter, return addresses, etc
0x03xx ...  last stack entry, your "int v" variable


0x02?? .... start of heap (__bss_end)
0x02?? .... last bss element,
0x02?? .... bss section start
0x0200 .... start of ram, also data section start



a more clear picture in this pdf




the __brkval and __heap_start are pointer to what i am more familiar w/


you could try to use __bbs_end instead of __heap_start and u should be able to compile. (i don't use energia but mspgcc compiles) 


don't now if there is a __brkval equivalent in energia (or what's beneath it). and if u don't use malloc(), u may want to just assume it's zero.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...