Tracking memory usage

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Tracking memory usage

Post by kazade »

I'm fast running out of memory when running my game engine, anyone have any tips on how to track where it's going?

I've started using malloc_stats but I don't really understand the output... :(
User avatar
lerabot
Insane DCEmu
Insane DCEmu
Posts: 134
Joined: Sun Nov 01, 2015 8:25 pm
Has thanked: 2 times
Been thanked: 19 times

Re: Tracking memory usage

Post by lerabot »

Check this topic from not too long ago, there's also some good link further down.
viewtopic.php?f=29&t=104322

Just keep in mind that if you,re freeing texture of PVR related stuff you need to use pvr_mem_free().
Since you work with -lgl (right?), glDeleteTexture() will work as well.

You can use pvr_mem_available() to track your PVR memory aswell.

You might have looked into this already but that's where I had my memory issue a week ago.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Tracking memory usage

Post by bogglez »

Do you mean VRAM or RAM usage?

For VRAM it's really important that you use texture compression and palettes as much as possible, this should help: http://dcemulation.org/?title=Texture_F ... compressed
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Tracking memory usage

Post by kazade »

I haven't got as far as filling up VRAM yet :D

Pretty sure it's RAM I'm running out of. My application is pretty heavy anyway (2.5M) then if I'm reading malloc_stats() right (I'm probably not!) it seems like glKosInit() eats a bunch of RAM, and then my game engine loads some mesh data and runs out... but I need to understand where all the memory is going between glKosInit and the crash.

I tried the example in your link @lerabot but I couldn't get it to compile (undefined reference to __end??). I just need a function to tell me roughly how much memory is left and then I can probably figure out what's going wrong.
111
DCEmu Junior
DCEmu Junior
Posts: 42
Joined: Thu Jul 07, 2016 7:11 pm
Has thanked: 0
Been thanked: 6 times

Re: Tracking memory usage

Post by 111 »

kazade wrote: I just need a function to tell me roughly how much memory is left and then I can probably figure out what's going wrong.
copypaste from my framework (based on that openbor code):

Code: Select all

/*////////////////////////////////////////////*/
#include <malloc.h>



static unsigned long systemRam = 0x00000000;
static unsigned long elfOffset = 0x00000000;
static unsigned long stackSize = 0x00000000;



extern unsigned long end;
extern unsigned long start;

#define _end end
#define _start start



void set_system_ram()
{
   systemRam = 0x8d000000 - 0x8c000000;
   elfOffset = 0x8c000000;

   stackSize = (int)&_end - (int)&_start + ((int)&_start - elfOffset);
}

unsigned long get_system_ram()
{
   return systemRam;
}


unsigned long get_free_ram()
{
    struct mallinfo mi = mallinfo();
    return systemRam - (mi.usmblks + stackSize);
}




void print_ram_stats()
{
	float sys_ram, free_ram, used_ram;
	sys_ram = (float)get_system_ram() / (float)(1024*1024);
	free_ram = (float)get_free_ram() / (float)(1024*1024);
	used_ram = (sys_ram - free_ram);
	
	printf("\n---------\nRAM stats (MB):\nTotal: %.2f, Free: %.2f, Used: %.2f\n---------\n", sys_ram, free_ram, used_ram);
}

than you have to call set_system_ram() once somewhere at the beginning of your program.
Works fine for me.

also, as I said before, don't bother using KGL, it's too much trouble than it worth it. PVR api is not that much hard to use anyway.
kazade
Insane DCEmu
Insane DCEmu
Posts: 145
Joined: Tue May 02, 2017 3:11 pm
Has thanked: 3 times
Been thanked: 34 times

Re: Tracking memory usage

Post by kazade »

Thanks that worked!

I do prefer using libGL as my engine is cross-platform and I can more easily test the GL1 renderer if I keep it compatible. However, it does seem that libGL uses a lot of RAM:

Code: Select all

Free RAM: 11329540 (/simulant/simulant/utils/memory.cpp:45)
pvr: enabling vertical scaling for non-VGA
OpenGL initialized (/simulant/simulant/kos_window.cpp:42)
Free RAM: 5562372 (/simulant/simulant/utils/memory.cpp:45)
I'll take a look and see if I can come up with a patch to reduce the memory usage.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Tracking memory usage

Post by bogglez »

KGL allocates vertex buffers into which it will write polygons before sending them off to the GPU. Maybe you want to change their size?
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
Post Reply