forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Way to detect RAM usage?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Fri Jan 06, 2006 6:32 am    Post subject: Way to detect RAM usage? Reply with quote

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
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Fri Jan 06, 2006 9:39 am    Post subject: Reply with quote

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
View user's profile Send private message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Sat Jan 07, 2006 2:37 am    Post subject: Reply with quote

Would either of my malloc-based ideas work?
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Sat Jan 07, 2006 2:54 am    Post subject: Reply with quote

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
View user's profile Send private message
Garak



Joined: 27 Jul 2005
Posts: 46

PostPosted: Sat Jan 07, 2006 5:48 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Sat Jan 07, 2006 11:44 pm    Post subject: Reply with quote

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
View user's profile Send private message
mrbrown



Joined: 17 Jan 2004
Posts: 1536

PostPosted: Sun Jan 08, 2006 12:02 am    Post subject: Reply with quote

Er, you only start out with a bit less than 24MB. So 1.6MB seems about right :).
Back to top
View user's profile Send private message
AnonymousTipster



Joined: 01 Jul 2005
Posts: 197

PostPosted: Sun Jan 08, 2006 12:38 am    Post subject: Reply with quote

Yes I meant for everything, including all the PSP native stuff. Ok, so I know that's the right value. Thanks.
Back to top
View user's profile Send private message
elz



Joined: 21 Nov 2005
Posts: 3

PostPosted: Mon Jan 09, 2006 6:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group