loadImage from graphics.h uses too much memory?

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

Moderators: cheriff, TyRaNiD

Post Reply
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

loadImage from graphics.h uses too much memory?

Post by r0b3rt »

Hi.

I'm using graphics.h (and .c) from http://www.psp-programming.com/tutorials/c/lesson04.htm (and png.h) to load a png image (32x32).
Before calling 'loadImage' 'sceKernelTotalFreeMemSize' tells me I have 21 MB of RAM free, and after calling the 'loadImage' function, I have only 1 MB (1907kB) of RAM free.
After that, no matter how many png images I load, I always have 1907kB of RAM free. And 'freeImage' doesn't free any RAM.

Is there a bug in graphics.c or png.c?
Is there a better and safer way to load and display a png image?
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

By default, the moment you allocate any memory, it will reserve the maximum possible memory for your application to further allocate from.

The actual Image structure will be only a few KB.
Use PSP_HEAP_SIZE_KB(value) to set the maximum amount of memory (in kilobytes) to reserve for your application.

If you use PSP_HEAP_SIZE_KB(1024) you can keep loading images upto 1MB but if you load anymore the PSP will crash.

If you are just making an EBOOT type application to run from the XMB, then just leave it at default. You only need to worry about reserving too much memory if you are making a plugin or PRX.

freeImage will only free the RAM *inside* the heap. The heap as a whole still exists and is reserved for your application.

In case you are making a plugin or PRX and need to free the RAM for other applications when you are done, use __psp_free_heap().

First declare the function prototype void __psp_free_heap(void);
Then call it when you need to clean up.
r0b3rt
Posts: 17
Joined: Mon May 26, 2008 6:43 am

Post by r0b3rt »

Thank you!

I also tried loading images using SDL and then tried just calling malloc in the main function - the result was the same. The entire RAM defined in PSP_HEAP_SIZE_KB wasn't free anymore. Then I tried loading an extenal prx, and it didn't work, because he had no memory to load.

Now, before loading the prx, I call __psp_free_heap(), and I also call it before exiting the prx, and everything works fine. So thanks once again, I now understand better how homebrews on PSP use memory.
Post Reply