How do I determine how much VRAM is available on my video card?

Q: How do I determine how much VRAM is available on my video card?

A: There are a number of ways to do this, but the most straight-forward method is via Core Graphics and IOKit. The first thing to do is to ask Core Graphics for the service port(s) for the online display(s) (video cards that currently have monitors attached to them). Once you have the port(s), you can then pass them off to IOKit with the proper key (IOFBMemorySize) to retrieve the amount of VRAM present on the card. IOKit will return a CFNumber, so the following code converts it to a standard data type using CFNumberGetValue().

Listing 1: Using Core Graphics and IOKit to retrieve VRAM size

-(int)vramSize:(long**)vsArray
{
    CGError                    err = CGDisplayNoErr;
    int                            i = 0;
    io_service_t              *dspPorts = NULL;
    CGDirectDisplayID    *displays = NULL;
    CGDisplayCount        dspCount = 0;
    CFTypeRef                 typeCode;

    // How many active displays do we have?
    err = CGGetActiveDisplayList(0, NULL, &dspCount);

    // Allocate enough memory to hold all the display IDs we have
    displays = calloc((size_t)dspCount, sizeof(CGDirectDisplayID));
    // Allocate enough memory for the number of displays we're asking about
    *vsArray = calloc((size_t)dspCount, sizeof(long));
    // Allocate memory for our service ports
    dspPorts = calloc((size_t)dspCount, sizeof(io_service_t));

    // Get the list of active displays
    err = CGGetActiveDisplayList(*dspCount,
                                 displays,
                                 &dspCount);

    // Now we iterate through them
    for(i = 0; i < dspCount; i++)
    {
        // Get the service port for the display
        dspPorts[i] = CGDisplayIOServicePort(displays[i]);
        // Ask IOKit for the VRAM size property
        typeCode = IORegistryEntryCreateCFProperty(dspPorts[i],
                                                   CFSTR(kIOFBMemorySizeKey),
                                                   kCFAllocatorDefault,
                                                   kNilOptions);

        // Ensure we have valid data from IOKit
        if(typeCode && CFGetTypeID(typeCode) == CFNumberGetTypeID())
        {
            // If so, convert the CFNumber into a plain unsigned long
            CFNumberGetValue(typeCode, kCFNumberSInt32Type, vsArray[i]);
            if(typeCode)
                     CFRelease(typeCode);
        }
    }
     // Return the total number of displays we found
     return (int)dspCount;
}

Note: The maximum number of displays possible is 32, as this is a limit imposed by Core Graphics. CGGetActiveDisplays() will only return the number qualifying displays in the dspCount variable, i.e. video cards without monitors will not be included.

IMPORTANT: This sample allocates memory to a pointer passed as a parameter. You MUST free this memory at some point in order to avoid leaking. Also note that error-checking code has been removed from this snippet. The complete code sample is available from the Video Hardware Info sample.

Document Revision History

DateNotes
2004-10-11Using Core Graphics and IOKit to find the physical size of VRAM on installed hardware.

Posted: 2004-10-11


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.