LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Memory usage of openGL program (https://www.linuxquestions.org/questions/programming-9/memory-usage-of-opengl-program-228072/)

cjp 09-08-2004 04:59 AM

Memory usage of openGL program
 
I have a question about memory usage in openGL:

I am the developer of Ultimate Stunts (http://ultimatestunts.sf.net ), and I have an ATI radeon 9200 card with 128MB video RAM, and using the closed-source drivers from ATI. I discovered with top that Ultimate Stunts needed up to 197 MB virtual memory, and I would like to know where that comes from.

First of all, that 197 MB is a stable number, so it's not a memory leak (at least not in the game loop). So I tried to turn everything off (texturing, z-buffering, detailed geometry, ...), and I managed to get the memory usage down to 155 MB.

Then I tried running glxgears, which needed 150 MB. So it seems that nothing's wrong with Ultimate Stunts. The extra 5MB can come from a bigger executable, and sound samples.

The next thing I tried was to turn every feature on step by step. The results showed that most features didn't increase the memory usage, but that fully enabling the textures needed an extra 39 MB.

I calculated the size of the bitmaps, and even with 32 bits per pixel it only was 28 MB.

The questions are:
1: why is that 150 MB for openGL so big?
2: Does that 150 MB include my 128 MB video memory?
3: I expect the textures to be loaded into video memory. If the video memory is already assigned to the process, then why does loading textures increase the memory usage? Is this a normal RAM cache of the openGL driver?
4: Why is the extra required memory for the textures not equal to the calculated size?

rjlee 09-08-2004 05:52 AM

1) Linux reports the video memory used by a program in addition to system RAM.
2&3) Video memory is dynamically allocated, just like system RAM, and the amount used grows as you put stuff into it. Otherwise, virtual desktops and virtual terminals (and even multiple apps on the same desktop) wouldn't work if you just allocated the whole lot to one process, because no other process would have any space to load its own bitmaps into.

To answer question 4, it depends on your hardware acceleration. But textures tend to be aligned on 32-bit boundaries even for 24-bit textures because this tends to make them faster to move around the graphics card. So you'll get 8 out of every 32 bits not being used.

foo_bar_foo 09-09-2004 01:02 AM

Hi,
i'm as far from an expert in this as one can get !!!!!
but i like thinking about it so here i go...
first -- all memory in linux is a virtual abstraction layer
a top level page table array points to second level pages
the card driver is using memory mapping to map a range of regular user space virtual memory pages to the pages mapped to the device memory by the driver ...
it may even be possible that for speed the driver is writing dummy data to the allocated pages ???
possibly bypassing malloc and instead allocating page-aligned memory which might show up bigger due to page size alignment or if using malloc allocating a larger size and mapping an alligned page size region within it -- since the driver is a kernel module it is even possible these pages are locked !
and will show up everywhere as a fixed size.
OK all of this stuff is used by x and not by your program i think
if you look at
/proc/X_pid/map i think the /dev/mem entries are the maps to the card space
so the video card memory can potentially show all used up all the time ???
this is such an abstraction i might have the /dev/names backwards ?
when x writes your program data to /dev/ati (or whatever the user space side of the shared mapping is)
i'm not even sure it has to even shows up as your programm using memory (it's just piping through to memory allocated to x) ?? or it may be showing up as your program using all video mem ?
it looks like the latter for you .

anybody else with a different understanding of that please speakup

so what you are seeing in top for your app is non video card user space virtual memory
that must have a page or pages allocated to 100% of the video mem...
you should be able to look at /proc/pid and see exactly what it all is........

one interesting point is that i just ran glxgears here on my nVidia system and i get
18.8 Mb Total
6.2 Mb RSS
15.0 Mb Shared

so mine is just showing what is being used on the user side.
but it's all just too close to the total i think to be a coincidence for yours......
my x server says 154Mb which is almost exactly what you are saying.....
it's really all in how the driver is written i think.

cjp 09-09-2004 06:28 AM

So as far as I understand it is possible that the 150 MB consists (partially) of memory mapped to the video memory, or shared memory with the X server & which is most probably driver specific. So I don't have to worry about that. I understand that memory alignment of the textures can take up some extra memory, which can make the difference between 28MB and 39MB.

Then one question remains: why does my memory usage increase when I load textures? I load them, give a pointer to openGL, openGL copies it to its own memory, and then I free it in my own memory. I expect the openGL driver to load it into video RAM. If this was not already present in that 150MB, then I'd like to know what IS in the 150MB. Maybe it's the X server's memory, maybe I'll have a look at that /proc/[id]/map.

One other interesting thing: I tried to run glxgears TWO times simultaneously, and they did not rotate simultaneously. Instead, first nr 1 rotated for maybe a second, then the other, then the first again etc. Is this a slow kind of multitasking to the video card resources?


All times are GMT -5. The time now is 02:14 PM.