Free memory after unloading PRX [SOLVED]

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

Moderators: cheriff, TyRaNiD

Post Reply
theHobbit
Posts: 65
Joined: Sat Sep 30, 2006 5:26 am

Free memory after unloading PRX [SOLVED]

Post by theHobbit »

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: Select all

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: Select all

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

Post by TyRaNiD »

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.
theHobbit
Posts: 65
Joined: Sat Sep 30, 2006 5:26 am

Post by theHobbit »

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: Select all

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


PSP_MODULE_INFO&#40;"PLUGIN", 0, 1, 1&#41;;
PSP_NO_CREATE_MAIN_THREAD&#40;&#41;;
PSP_HEAP_SIZE_KB&#40; 512 &#41;;

#ifdef __cplusplus
extern "C" &#123;
#endif
int module_start&#40;SceSize argc, void* argp&#41;
&#123;
 	return 0;
&#125;

int module_stop&#40;int args, void *argp&#41;
&#123;
	return 0;
&#125;
#ifdef __cplusplus
&#125;
#endif

/* Prototypes */
void init&#40;void&#41;;
void cleanup&#40; void &#41;;
void work&#40;void&#41;;

/* End Prototypes */

/* Variables */


/** START of Plugin definitions setup */
Plugin table = 
&#123;	
	init,	
   work,				
	cleanup									
&#125;;
/** END of Plugin definitions setup */

/** Single Plugin Export */
Plugin *get_plugin_info&#40;&#41;
&#123;
	return &table;
&#125;

void init&#40;void&#41;
&#123;	
	// initialize all stuff
   // allocate some memory
&#125;


void work&#40;void&#41;
&#123;
   // do something
&#125;

void cleanup&#40; void &#41;
&#123;
	//free all stuff
&#125;
theHobbit
Posts: 65
Joined: Sat Sep 30, 2006 5:26 am

Post by theHobbit »

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!!!!
theHobbit
Posts: 65
Joined: Sat Sep 30, 2006 5:26 am

Post by theHobbit »

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!!
theHobbit
Posts: 65
Joined: Sat Sep 30, 2006 5:26 am

Post by theHobbit »

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.
Post Reply