Difference between revisions of "Arduino notes"

From Noah.org
Jump to: navigation, search
m (Created page with 'Category:Engineering == debug memory problems (out of RAM) == See [http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/20 Arduino Forum on memory usage]. This functi…')
 
m (debug memory problems (out of RAM))
Line 7: Line 7:
 
<pre>
 
<pre>
 
int freeRam () {
 
int freeRam () {
   extern int __heap_start, *__brkval;  
+
  // __brkval is the address of the top of the heap if memory has been allocated.
 +
  // If __brkval is zero then it means malloc has not used any memory yet, so
 +
  // we look at the address of __heap_start.
 +
   extern int __heap_start
 +
  extern int *__brkval; // address of the top of heap
 
   int stack_top;  
 
   int stack_top;  
   return (int) &stack_top - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);  
+
   return (int)&stack_top - ((int)__brkval == 0 ? (int)&__heap_start : (int)__brkval);  
 
}
 
}
  
 
Serial.println(freeRam());
 
Serial.println(freeRam());
 
</pre>
 
</pre>

Revision as of 15:42, 30 July 2012


debug memory problems (out of RAM)

See Arduino Forum on memory usage. This function will return the amount of RAM free. This accounts for the stack and heap used.

int freeRam () {
  // __brkval is the address of the top of the heap if memory has been allocated.
  // If __brkval is zero then it means malloc has not used any memory yet, so
  // we look at the address of __heap_start.
  extern int __heap_start
  extern int *__brkval; // address of the top of heap
  int stack_top; 
  return (int)&stack_top - ((int)__brkval == 0 ? (int)&__heap_start : (int)__brkval); 
}

Serial.println(freeRam());