Query grahpics memory stats

I need a way to programatically (C/C++) determine the total amount of graphics memory and the amount of free graphics memory in Linux (and maybe other UNIX platforms as well). Does anybody know how to do that?

Thanks in advance.

cat /var/log/Xorg.0.log|grep -i nvidia|grep memory:

This gives you the total amount of memory as the driver reports it. As you see this is only for NVIDIA cards, I don’t know anything about ATI. You should be able to translate this to C/C++ easily.
AFAIK querying the amount of free memory is not possible.

satan, I tried your solution. I need “|grep Memory” instead of “|grep memory” at this end.

Here is a solution that works for me:

  • For nVidia, for the total amount of graphics memory only:
  1. download nvidia-settings at:
    ftp://download.nvidia.com/XFree86/nvidia-settings/
  2. there is some code example here:
    nvidia-settings-1.0/samples/nv-control-info.c

This example uses the NVCtrl library. It gives access to the NV-CONTROL X extension. (X extension, not OpenGL extension)
Look for the part that query attribute NV_CTRL_VIDEO_RAM, it returns the amount of VRAM in kB.

you first need a Display connection:

Display *dpy=XOpenDisplay(NULL);

then check if the X extension is available on the X server:

if(XNVCTRLQueryExtension(dpy,&eventBase,&errorBase)==True)

iterate over all the screens:

int screenCount=XScreenCount(dpy);
int i=0;
while(i<screenCount)
{

check if a screen is managed by nVidia:

if(XNVCTRLIsNvScreen(dpy,i))

if so, get the amount of VRAM in kB:

int ramSize;
Bool status=XNVCTRLQueryAttribute(dpy,i,0, NV_CTRL_VIDEO_RAM,&ramSize);

then you can install the library yourself from this archive (the relevant files are in src/libXNVCtrl) or
rely on your Linux distribution. For example, Ubuntu package
“nvidia-settings” provides libXNVCtrl.a in /usr/lib/ and the header files NVCtrlLib.h and NVCtrl.h in /usr/include/NVCtrl/

Regarding the license, pick a version not older than 177.82:
prior to 177.82, nVidia picked the GPL by mistake. Now the code is MIT-X11 / BSD.

  • For ATI (never tried), there is the following recent (March 2009) OpenGL extension, for the free memory only:

http://www.opengl.org/registry/specs/ATI/meminfo.txt

Thanks for the tip. Sifting headers, there’s lots of good GPU info you can query and set through that extension.

Yet another fun way to do this with NV…

$ nvidia-settings --query=[gpu:0]/VideoRam

Use

$ nvidia-settings -e all

to get a list of all attributes.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.