9

How to get total memory in bytes used by OpenGL in C++?

I'm building an OpenGL application and the total memory used seems to be rising, I can get the info about the total memory used by variables & objects created by myself but can't guarantee how much memory OpenGL is using for its variables & objects & textures, etc. So is it possible to get the total memory in bytes used by OpenGL in C++?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
jondinham
  • 8,271
  • 17
  • 80
  • 137

1 Answers1

8

In general, you don't. OpenGL is ultimately a hardware abstraction. And OpenGL simply doesn't provide a way to get that sort of information.

There are vendor-specific extensions that will give you ways to ask, though what you get back depends on the architecture. AMD hardware provides the ATI_meminfo extension. It breaks memory down into types of objects: buffer objects, textures, and renderbuffers.

NVIDIA provides the experimental extension NVX_gpu_memory_info. There's no information in the registry about how to use it, so I can't link you to anything.

In any case, the most effective way to know what the GPU is using is to just keep track of it yourself. Always use internal image formats with sizes; this means you can compute a pretty good estimate of how much memory a texture takes up. The same goes for buffer objects and so forth.

You won't get exact numbers, as padding, alignment, and the like can confound you. But you'll get something pretty decent.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • I think that also texture mipmaps shall be taken into account for memory usage statistic. – Luca Oct 04 '11 at 07:53
  • +1 Good answer. I actually didn't answer this question because I got the impression that the OP wants to know the CPU memory used by the OpenGL implementation (driver). As otherwise (if refering to GPU memory) I don't know how he should have seen that "the total memory used seems to be rising". – Christian Rau Oct 04 '11 at 12:24
  • i'm using mipmaps and this should take memory 50% more than using bitmap textures. but GL seems to use both RAM and Video RAM – jondinham Oct 05 '11 at 03:57
  • 2
    @Paul: A mipmapped texture takes up ~33% as much as a texture without mipmaps (unless it's a 1D or 3D texture). – Nicol Bolas Oct 05 '11 at 05:00
  • 1
    50% for 1D, 33% for 2D and 25% for 3D. – Macke Feb 07 '14 at 08:05