Is it possible to modfiy freeRam() to find the percent of freeRam that is fragmented?
To my understanding, freeRam finds out the total amount of free ram available, but it doesn't tell you jack about the largest space available which is frustrating.
I've seen a lot of post where people think fragmentation is the problem of their crashes, and it probably is, but I realize how helpful freeRam() is, and I think this would be a very helpful piece of code for a lot of people.
Heres what I was thinking create a function called fragmentedRam() which first uses freeRam() to find out total freeRam finds and then it runs a modified verson of freeRam which finds the largest whole chunk of memory available. So if freeRam() returned 100, and the modifed verson of freeRam found that the largest chunk of whole memory was 80 then it would find the percentage of fragmentedRam by 100/(100-80) which is 20 then it would return that value.
To sum it up, fragmentedRam() returns the amount of freeRam fragmented, or you could make it so it returns 80 so you know the actual largest chunk size of memory you have left to play with.
I'm still learning though, I don't quite understand how freeRam works, so I cant modify it myself yet. Is this possible to do though? Here are my notes that i've taken from Bucky's Buckys youtube c++ classes
void loop() {}
int freeRam() {
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);
}
/*
// extern means the file is located in an external library
// & is an address reference. When you see & it isn't returning a copy, it's sending the acutal address
// * is called pointer and is basically memory address and is needed before the name of an int is to send the reference
// example
void passByReference(int *x);
int main()
{
int betty = 13;
int sandy = 13;
passByValue(betty);
passByReference(&sandy); // symbol for memory address long hex address on computer
Serial.print(F("betty is now"));
Serial.println(betty);
Serial.print(sandy is now);
Serial.println(sandy);
// Serial monitor will print betty is now 13
// sandy is now 66
}
void pass ByValue(int x)
{ x = 99;
}
void passByReference(int *x) //
{
*x=66;
}
*/
/* another thing to remember, if you increment address pointer (*) it doesn't change the address it changes the element that it points to
int main(){
int bucky[5];
int *bp0 = &bucky[0];
int *bp1 = &bucky[1];
int *bp0 = &bucky[2];
Serial.print(F("bp0 is at "));
Serial.println(bp0);
Serial.print(F("bp1 is at "));
Serial.println(bp1);
Serial.print(F("bp2 is at "));
Serial.println(bp2);
bp0+=2;
Serial.print(F("bp0 is now at "));
Serial.println(bp0);
// Serial monitor will say bp0 is at 0x28ff00 // each int takes up four bytes
// bp1 is at 0x28ff04
// bp2 is at 0x22ff08
// bp0 is at 0x28ff08 // it didn't increment address, it only incremented element. so intead of adding 2 to the address itsef, it looked for 2 past the current element
*/
I also know that anything with a __ in front of it is private, or suppose to be anyway.
So using
int freeRam() {
extern int
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);
}
I know that int freeRam() returns an int.
extern int means that it's an int from an extern library (probably arduino.h) but its strange that it doesn't list the name of the int beside it...
int v creates an int but the interesting part is the bottom line
freeRam() returns the memory address of v (which it just created) minus... and that's as much as I know right now. I know (int)&__heap_start is the memory address of the heap start but I don't understand how the red below works.
(__brkval == 0 ? (int)&__heap_start : (int) __brkval); I haven't finished the videos yet, i'm only on number 48, but I figured I'd go ahead and ask if this was possible, and if anyone would be interested in helping.