Hello Community,
i want to create a memory checkup on my Due do get RAM informetions about free ram, used heap and used stack.
This works for stack, but not for heap memory. I got memory adresses after calloc() nearby the adresses of stack adresses.
global variables definition:
unsigned long FirstAdressHeap;
unsigned long FirstAdressStack;
unsigned long previousMillisMemoryStatePrint;
const unsigned long WaitingTimeMemoryStatePrint = 10000;
running one time at begin in setup():
FirstAdressHeap = getLatestHeapAdress();
FirstAdressStack = getLatestStackAdress();
running in loop:
if (millis() - previousMillisMemoryStatePrint > WaitingTimeMemoryStatePrint)
{
PrintRAMstate();
previousMillisMemoryStatePrint = millis();
}
Defined functions:
void PrintRAMstate()
{
unsigned long currentHeapSize;
unsigned long currentStackSize;
unsigned long currentFreeRAM;
currentHeapSize = getLatestHeapAdress() - FirstAdressHeap;
currentStackSize = FirstAdressStack - getLatestStackAdress();
currentFreeRAM = getFreeRam();
Serial.print(stringDebugState + "currentHeapSize: ");
Serial.print(currentHeapSize);
Serial.print(" currentStackSize: ");
Serial.print(currentStackSize);
Serial.print(" currentFreeRAM: ");
Serial.println(currentFreeRAM);
}
unsigned long getFreeRam()
{
int* heapVar = (int*)malloc(sizeof(int));
unsigned long stackVarAndHeapAdress = (unsigned long)&heapVar;
free(heapVar);
return (unsigned long)&stackVarAndHeapAdress - stackVarAndHeapAdress;
}
unsigned long getLatestStackAdress()
{
byte stackVar;
Serial.print(stringDebugState + "Stack Adress got: ");
Serial.println((unsigned long)&stackVar);
return (unsigned long)&stackVar;
}
unsigned long getLatestHeapAdress()
{
int* heapVar=(int*)calloc(1,sizeof(int));
unsigned long heapAdress = (unsigned long)&heapVar;
free(heapVar);
Serial.print(stringDebugState + "Heap Adress got: ");
Serial.println(heapAdress);
return heapAdress;
}
This prints out in serial monitor (not plausible):
[Debug state] Heap Adress got: 537427840
[Debug state] Stack Adress got: 537427852
[Debug state] currentHeapSize: 4294967240 currentStackSize: 56 currentFreeRAM: 4
Old solutions from about 2016 are not compiling with Due, like this:
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
Compile-Error prints in console:
Compiling 'SOAP_Server' for 'Arduino Due (Programming Port)'
Error linking for board Arduino Due (Programming Port)
Build failed for project 'SOAP_Server'
SOAP_Server.cpp.o: In function freeRam()
SOAP_Server.ino:2087: undefined reference to __brkval
SOAP_Server.ino:2087: undefined reference to __heap_start
collect2.exe*: error: ld returned 1 exit status
Does anybody have an idea, how i can get this thee memory informations in running program?