Way to detect RAM usage?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Way to detect RAM usage?

Post by AnonymousTipster »

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.
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

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 :)
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Would either of my malloc-based ideas work?
TyRaNiD
Posts: 907
Joined: Sun Jan 18, 2004 12:23 am

Post by TyRaNiD »

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.
Garak
Posts: 46
Joined: Wed Jul 27, 2005 1:59 am

Post by Garak »

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
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Ok, i've written a simple little function, which seems to work:

Code: Select all

float GetRAMFree(){
	float ram;
//a hacky little way to estimate RAM left to use	
int ramAdd[320];
int i=0;
for&#40;i=0;i<320;i++&#41;&#123;
ramAdd&#91;i&#93; = malloc&#40;100000&#41;;
if&#40;ramAdd&#91;i&#93; == 0&#41;&#123;//malloc failed
ram = &#40;float&#41;i;
int z=0;
for&#40;z=0;z<i;z++&#41;&#123;
free&#40;ramAdd&#91;z&#93;&#41;;
&#125;
break;
&#125;
&#125;
return ram/10;

&#125;
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?
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Er, you only start out with a bit less than 24MB. So 1.6MB seems about right :).
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Post by AnonymousTipster »

Yes I meant for everything, including all the PSP native stuff. Ok, so I know that's the right value. Thanks.
elz
Posts: 3
Joined: Mon Nov 21, 2005 8:10 pm

Post by elz »

AnonymousTipster wrote:Ok, i've written a simple little function, which seems to work:

Code: Select all

float GetRAMFree&#40;&#41;&#123;
	float ram;
//a hacky little way to estimate RAM left to use	
int ramAdd&#91;320&#93;;
int i=0;
for&#40;i=0;i<320;i++&#41;&#123;
ramAdd&#91;i&#93; = malloc&#40;100000&#41;;
if&#40;ramAdd&#91;i&#93; == 0&#41;&#123;//malloc failed
ram = &#40;float&#41;i;
int z=0;
for&#40;z=0;z<i;z++&#41;&#123;
free&#40;ramAdd&#91;z&#93;&#41;;
&#125;
break;
&#125;
&#125;
return ram/10;

&#125;
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...
Post Reply