Way to detect RAM usage?
-
- Posts: 197
- Joined: Fri Jul 01, 2005 2:50 am
Way to detect RAM usage?
Is there a way to work out how much RAM the program is currently using?
I was thinking of doing a function that mallocs data until it errors, then freeing the data, and the amount of data could be worked out from how many mallocs were used.
Could it also be possible to get the address of a malloc, and compare it with the starting RAM address to work out the consumption?
What i'd like to know, is what is the best way of finding out how much RAM your app has left to use?
Thanks.
I was thinking of doing a function that mallocs data until it errors, then freeing the data, and the amount of data could be worked out from how many mallocs were used.
Could it also be possible to get the address of a malloc, and compare it with the starting RAM address to work out the consumption?
What i'd like to know, is what is the best way of finding out how much RAM your app has left to use?
Thanks.
There is I guess functions such as malinfo in newlib which allows you to get how many free blocks you have, but that will only tell you what you have which has already been "sbrk'ed", then there is sbrk itself which by passing 0 returns you the current heap pointer, however there is no way of then knowing how big the heap was to start with or its base address (at least trivally). So the answer is, not really. It would have to be something inside newlib to make it work, which there currently isn't, and even then you would be limited to probably only really knowing how much heap you haven't currently set aside for malloc which isn't necessarily the same thing :)
-
- Posts: 197
- Joined: Fri Jul 01, 2005 2:50 am
Sure you probably could but it wouldn't be the prettiest thing ever created. Might be best to see if there is a simple way of integrating some stuff into newlib to extract the heapsize and heap base, then use mallinfo, sbrk and that info to give you an idea of home much you have left. Alternatively you could specify your heap size manually and work it out from that.
As said above, the malloc ideo should work, but it won't be the prettiest. If your malloc fails, it will return 0, then you know you are out of memory.
But... beware. I am wondering if it would be a problem if you allocated a chunk of memory that was too big (like a 10 meg chunk). Maybe the system still has 10 megs left, but cannot organize what is in ram to accomodae a consecutive 10 meg chunk of memory. Maybe a mad mallocing man expert could answer that for you.
But alas, if you simply attempt to malloc "small" amounts of memory (like 1 meg chunks), you know you will only need a max of 32 mallocs before you run out of ram. Then you could fine tune the size of your last malloc to get the exact amount. This would still be a pretty slow process, so unless your app has lots of time to burn, I would not recommend doing it too often. And you better free all the memory once you finish testing it.... or else.
Garak
But... beware. I am wondering if it would be a problem if you allocated a chunk of memory that was too big (like a 10 meg chunk). Maybe the system still has 10 megs left, but cannot organize what is in ram to accomodae a consecutive 10 meg chunk of memory. Maybe a mad mallocing man expert could answer that for you.
But alas, if you simply attempt to malloc "small" amounts of memory (like 1 meg chunks), you know you will only need a max of 32 mallocs before you run out of ram. Then you could fine tune the size of your last malloc to get the exact amount. This would still be a pretty slow process, so unless your app has lots of time to burn, I would not recommend doing it too often. And you better free all the memory once you finish testing it.... or else.
Garak
-
- Posts: 197
- Joined: Fri Jul 01, 2005 2:50 am
Ok, i've written a simple little function, which seems to work:
Which returns a value of about 22.4 MB free when called in the cube GU sample. Does this value seem about right, or is 9.6MB too much for the sample to use up?
Code: Select all
float GetRAMFree(){
float ram;
//a hacky little way to estimate RAM left to use
int ramAdd[320];
int i=0;
for(i=0;i<320;i++){
ramAdd[i] = malloc(100000);
if(ramAdd[i] == 0){//malloc failed
ram = (float)i;
int z=0;
for(z=0;z<i;z++){
free(ramAdd[z]);
}
break;
}
}
return ram/10;
}
-
- Posts: 197
- Joined: Fri Jul 01, 2005 2:50 am
In my app it returns 17mb all the time... When im playing an mp3 or something it should return less... but it doesnt... always 17...AnonymousTipster wrote:Ok, i've written a simple little function, which seems to work:Which returns a value of about 22.4 MB free when called in the cube GU sample. Does this value seem about right, or is 9.6MB too much for the sample to use up?Code: Select all
float GetRAMFree(){ float ram; //a hacky little way to estimate RAM left to use int ramAdd[320]; int i=0; for(i=0;i<320;i++){ ramAdd[i] = malloc(100000); if(ramAdd[i] == 0){//malloc failed ram = (float)i; int z=0; for(z=0;z<i;z++){ free(ramAdd[z]); } break; } } return ram/10; }