Get ram usage data

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Get ram usage data

Post by Almamu » Thu Jun 27, 2013 3:30 pm

I'm working on a game and I've come to a point that I do not have enough memory left (still dont know if this is true at all or is some bad pointer which is messing the things up a bit).

Is there any function/structure to know how much RAM is used and how much RAM is free?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Get ram usage data

Post by elhobbs » Thu Jun 27, 2013 5:25 pm

try searching for mallinfo.

keep in mind that on a system with limited resources like the ds you can run into issue where you have enough free memory but it is fragmented - so you do not have a free chunk that is big enough to satisfy a malloc request. it can even come down to the order that objects are allocated. In general you want to make sure that you allocate large static objects first. if you are using malloc/free a lot then you may need to look into a custom allocator.

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get ram usage data

Post by Almamu » Thu Jun 27, 2013 8:28 pm

elhobbs wrote:try searching for mallinfo.

keep in mind that on a system with limited resources like the ds you can run into issue where you have enough free memory but it is fragmented - so you do not have a free chunk that is big enough to satisfy a malloc request. it can even come down to the order that objects are allocated. In general you want to make sure that you allocate large static objects first. if you are using malloc/free a lot then you may need to look into a custom allocator.
Do you know of any good free allocator or any documentation on how them work?

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get ram usage data

Post by Almamu » Thu Jun 27, 2013 9:28 pm

Sorry, for double-posting but the forum doesnt let me edit my last message...

I've added some debug to show the info from mallinfo and this is the output:

Code: Select all

Before trying to allocate space for texture data
Memory: 1370044 1354084 15960

After trying (failed) to allocate texture data
Memory: 1370044 1354084 15960

Before trying to allocate a smaller texture:
Memory: 51132 48580 2552

After allocating the small texture:
Memory: 51132 48660 2472
This is what I'm using to show the mallinfo data:

Code: Select all

Com_Printf("Memory: %d %d %d\n", mallinfo().arena, mallinfo().uordblks, mallinfo().fordblks);
Any idea?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Get ram usage data

Post by elhobbs » Fri Jun 28, 2013 1:18 am

take a step back.

how much memory are you trying to allocate? the ds only has 4MB of ram that is shared with both code and data. use the arm-none-eabi-size.exe program to see how much code and static data you have. that will let you know how much main ram will be available for use with malloc. is it even possible to fit everything you want in memory?

your print is essentially total, used, and free respectively. where the total is how much memory that the system has reserved for malloc - this will grow as needed to satisfy your malloc requests up until the all of the system main memory has been reserved. the used amount reflects how much of the total is currently allocated to the program. free represents how much of the total is still free. if malloc does not have enough memory in free (or a big enough continuous block in free) it will try to grow the total. if that succeeds then malloc will return a block of memory - if it cannot add more to the total then malloc will fail.

malloc failed for you with the total at 1370044 with 15960 free. since the first number is really low - I would expect closer to 2 MB or even 3 MB before a fail. how much memory were you trying to allocate when it failed? do you have a lot of static global data that might be using a lot of space? what were the results from arm-none-eabi-size?

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get ram usage data

Post by Almamu » Fri Jun 28, 2013 7:32 am

elhobbs wrote:take a step back.

how much memory are you trying to allocate? the ds only has 4MB of ram that is shared with both code and data. use the arm-none-eabi-size.exe program to see how much code and static data you have. that will let you know how much main ram will be available for use with malloc. is it even possible to fit everything you want in memory?

your print is essentially total, used, and free respectively. where the total is how much memory that the system has reserved for malloc - this will grow as needed to satisfy your malloc requests up until the all of the system main memory has been reserved. the used amount reflects how much of the total is currently allocated to the program. free represents how much of the total is still free. if malloc does not have enough memory in free (or a big enough continuous block in free) it will try to grow the total. if that succeeds then malloc will return a block of memory - if it cannot add more to the total then malloc will fail.

malloc failed for you with the total at 1370044 with 15960 free. since the first number is really low - I would expect closer to 2 MB or even 3 MB before a fail. how much memory were you trying to allocate when it failed? do you have a lot of static global data that might be using a lot of space? what were the results from arm-none-eabi-size?
This is the ouput of the arm-none-eabi-size:

Code: Select all

   text    data     bss     dec     hex filename
 257948    4212   99896  362056   58648 build_ds.elf
I'm trying to allocate 524288 bytes to load a 512x512 texture from nitroFS (allocation which fails as there is only 16840 bytes free...).

The problem is that I don't have a lot of data in memory, just some small structures that probably don't use even 50Kb of RAM, a cmap of 9KB and what NitroEngine allocates for textures (I only load 3 textures).

I've optimized the things a bit (basically got rid off a library that is not needed yet). This is the new output:

Code: Select all

Before trying to allocate space for texture data
Memory: 1370948 1354108 16840

After trying (failed) to allocate texture data
Memory: 1370948 1354108 16840

Before trying to allocate a smaller texture:
Memory: 52036 48604 3432

After allocating the small texture:
Memory: 52036 48684 3352

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get ram usage data

Post by Almamu » Fri Jun 28, 2013 7:52 am

I cannot edit last post so I'll make a new one:

I've run the example on different DS to see if the data changes, these are the results:

3DS and DSi (DSMode)

Code: Select all

Before trying to allocate space for texture data
Memory: 1432324 1420460 11864

After trying (failed) to allocate texture data
Memory: 1432324 1420460 11864

Before trying to allocate a smaller texture:
Memory: 117508 114956 2552

After allocating the small texture:
Memory: 117508 115036 2472
DSi (DSi Mode)

Code: Select all

Before trying to allocate space for texture data
Memory: 1432324 1420460 11864

After trying (failed) to allocate texture data
Memory: 1432324 1420460 11864

Before trying to allocate a smaller texture:
Memory: 117508 114956 2552

After allocating the small texture:
Memory: 117508 115036 2472
I though that this last would work, but It still has the same issue... DSi mode is supposed to have around 16MB of RAM, right?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Get ram usage data

Post by elhobbs » Fri Jun 28, 2013 11:52 am

Did you verify the amount you are trying to allocate when it fails? Do you have any code that I could look at?

Almamu
Posts: 25
Joined: Tue Apr 07, 2009 8:45 pm

Re: Get ram usage data

Post by Almamu » Fri Jun 28, 2013 12:28 pm

elhobbs wrote:Did you verify the amount you are trying to allocate when it fails? Do you have any code that I could look at?
Yep, I added some debug and NitroEngine tries to allocate 524288 bytes to load a texture. The image is a BMP of 24 bits, 512x512. I can send you all the code (Makefile, source, include, nitrofiles and nitroengine folders) by PM if you want to take a look at it.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Get ram usage data

Post by elhobbs » Sat Jun 29, 2013 1:16 am

ok - you are not failing to allocate memory using malloc. rather it is NE_Alloc that is failing. NE_Alloc in this case is trying to allocate from VRAM - which you have configured to use VRAM A-D for textures. which is 512K in total. the texture you are trying to load is way too big for the ds. you could get it into VRAM but it would be the only texture that you could load.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 20 guests