| View previous topic :: View next topic |
| Author |
Message |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Fri Jan 06, 2006 6:32 am Post subject: 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. |
|
| Back to top |
|
 |
TyRaNiD
Joined: 18 Jan 2004 Posts: 918
|
Posted: Fri Jan 06, 2006 9:39 am Post subject: |
|
|
| 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 :) |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Sat Jan 07, 2006 2:37 am Post subject: |
|
|
| Would either of my malloc-based ideas work? |
|
| Back to top |
|
 |
TyRaNiD
Joined: 18 Jan 2004 Posts: 918
|
Posted: Sat Jan 07, 2006 2:54 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
Garak
Joined: 27 Jul 2005 Posts: 46
|
Posted: Sat Jan 07, 2006 5:48 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Sat Jan 07, 2006 11:44 pm Post subject: |
|
|
Ok, i've written a simple little function, which seems to work:
| Code: | 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;
} | 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? |
|
| Back to top |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Sun Jan 08, 2006 12:02 am Post subject: |
|
|
| Er, you only start out with a bit less than 24MB. So 1.6MB seems about right :). |
|
| Back to top |
|
 |
AnonymousTipster
Joined: 01 Jul 2005 Posts: 197
|
Posted: Sun Jan 08, 2006 12:38 am Post subject: |
|
|
| Yes I meant for everything, including all the PSP native stuff. Ok, so I know that's the right value. Thanks. |
|
| Back to top |
|
 |
elz
Joined: 21 Nov 2005 Posts: 3
|
Posted: Mon Jan 09, 2006 6:47 pm Post subject: |
|
|
| AnonymousTipster wrote: | Ok, i've written a simple little function, which seems to work:
| Code: | 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;
} | 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? |
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... |
|
| Back to top |
|
 |
|