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 

Free memory after unloading PRX [SOLVED]

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



Joined: 30 Sep 2006
Posts: 65

PostPosted: Fri Mar 14, 2008 12:18 am    Post subject: Free memory after unloading PRX [SOLVED] Reply with quote

Hi there, i'm working on a program that loads multiple prxs like plugins.
but i have found that after unloading a prx not all the memory used is freed.

A i'm working on a SLIM with the PSP_LARGE_MEMORY option.
So after loading all the graphics and sound effects i have around 37718 KB of free memory (calling sceKernelTotalFreeMemSize).

Then I load a module with this:

Code:

SceUID id = -1;
   SceKernelLMOption option;
   SceUID mpid = PSP_MEMORY_PARTITION_USER;

   memset(&option, 0, sizeof(option));
   
   option.size = sizeof(option);
   option.mpidtext = mpid;
   option.mpiddata = mpid;
   option.position = 0;
   option.access = 1;

   SceSize am = sceKernelTotalFreeMemSize();
   Log( LOG_MESSAGE, "PRXLoader::Load('%s') - Available memory before loading: %dbytes (%dKB or %dMB)", filename, am, am/1024, am/1024/1024 );

   id = sceKernelLoadModule(filename, 0, &option);
sceKernelStartModule(id, 0, 0, 0, 0);


And sceKernelTotalFreeMemSize returns around 37559 KB after loading the module.

Now i stop and unload the module like this:

Code:

sceKernelStopModule( id, 0, NULL, &status, NULL );
sceKernelUnloadModule( id );


And sceKenelTotalFreeMemSize returns 36662 KB (when it should be around 37718 KB, that was the free memory before loading the plugin).

So anyone knows what else must be done to free all the memory used by the module?

Well thanks in advance!


Last edited by theHobbit on Sat Mar 15, 2008 12:51 am; edited 1 time in total
Back to top
View user's profile Send private message
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Fri Mar 14, 2008 4:22 am    Post subject: Reply with quote

If your prx has a main function you need to get it to exit in the stop function rather than letting the psp just clean it up. If it doesn't well you need to maintain a list of allocations and clean up properly.
Back to top
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Fri Mar 14, 2008 6:44 am    Post subject: Reply with quote

Thanks for the answer, well the plugin has no main function. I have a cleanup function that should be freeing all the memory allocated, well i guess i'll try with a simple malloc and free and see if all the memory is freed.

Oh i forgot to put a sample of a plugin:

i call the cleanup function before stopping and unloading the module.

Code:

#include <pspkernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>


PSP_MODULE_INFO("PLUGIN", 0, 1, 1);
PSP_NO_CREATE_MAIN_THREAD();
PSP_HEAP_SIZE_KB( 512 );

#ifdef __cplusplus
extern "C" {
#endif
int module_start(SceSize argc, void* argp)
{
    return 0;
}

int module_stop(int args, void *argp)
{
   return 0;
}
#ifdef __cplusplus
}
#endif

/* Prototypes */
void init(void);
void cleanup( void );
void work(void);

/* End Prototypes */

/* Variables */


/** START of Plugin definitions setup */
Plugin table =
{   
   init,   
   work,            
   cleanup                           
};
/** END of Plugin definitions setup */

/** Single Plugin Export */
Plugin *get_plugin_info()
{
   return &table;
}

void init(void)
{   
   // initialize all stuff
   // allocate some memory
}


void work(void)
{
   // do something
}

void cleanup( void )
{
   //free all stuff
}
Back to top
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Fri Mar 14, 2008 8:26 am    Post subject: Reply with quote

Ok, this is what i have found after doing some simple test:

- When i use malloc inside the prx to allocate some memory, even if
I call free before unloading the prx the memory is not really freed. Maybe the free function is not working properly? Well it's the same with
the new and delete operators.

Do I have to write my own malloc and free to handle the memory allocations? Hope anyone can help!!!!
Back to top
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Fri Mar 14, 2008 11:54 am    Post subject: Reply with quote

Ok, finally doing some more research i have found that the memory missing after unloading the prx is equal to the heap size of the prx.

For example: i have 23980 kb of free memory before loading the prx,
the prx have 1024 kb in the heap (with PSP_HEAP_SIZE_KB( 1024 )), now if i make a call to malloc to allocate some memory in the prx (even if its less than 1024 kb) and free the memory allocated right after, when unloading the prx, i have 22956 kb of free memory, wich is 1024 kb less. So anyone know how to solve this? thanks in advance!!
Back to top
View user's profile Send private message
theHobbit



Joined: 30 Sep 2006
Posts: 65

PostPosted: Sat Mar 15, 2008 12:57 am    Post subject: Reply with quote

Hi there, again...

Well i think i have found the solution, inside the prx cleanup function i make a call to the __psp_free_heap() function and yep there is no more memory missing in the main app!. Well i dont know yet if its safe to call that function. Well hope this helps somebody in a far away universe.
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