Skip to content

Instantly share code, notes, and snippets.

@Marzogh
Last active October 10, 2016 00:01
Show Gist options
  • Save Marzogh/a13242e1e751457b0e3f to your computer and use it in GitHub Desktop.
Save Marzogh/a13242e1e751457b0e3f to your computer and use it in GitHub Desktop.
This function places the current value of the heap and stack pointers in the variables. You can call it from any place in your code and save the data for outputting or displaying later. This allows you to check at different parts of your program flow. The stack pointer starts at the top of RAM and grows downwards. The heap pointer starts just ab…
//This function returns the amount of RAM currently in available
uint8_t * heapptr, * stackptr;
void check_mem() {
stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
heapptr = stackptr; // save value of heap pointer
free(stackptr); // free up the memory again (sets stackptr to 0)
stackptr = (uint8_t *)(SP); // save value of stack pointer
Serial.print(F("Heap: "));
Serial.println(*heapptr);
Serial.print(F("Stack: "));
Serial.println(*stackptr);
}
//This function returns the amount of RAM currently in use
int getMemory() {
int size = 1024; // Use 2048 with ATmega328
byte *buf;
while ((buf = (byte *) malloc(--size)) == NULL);
free(buf);
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment