11

Is there an easy and reliable way to get the total amount of the physical GPU memory?

I have tried this, but the problem is it returns 4096MB and I'm using a GTX 780 with 6144MB, so yeah not displaying correctly.

Code:

using System.Management;        

private void getGpuMem()
{
    ManagementClass c = new ManagementClass("Win32_VideoController");
    foreach (ManagementObject o in c.GetInstances())
    {
        string gpuTotalMem = String.Format("{0} ", o["AdapterRam"]);
        Debug.Write(gpuTotalMem);
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Patrik Fröhler
  • 1,221
  • 1
  • 11
  • 38
  • 2
    As I consider `Win32_VideoController` returns max 4gb because it is win32 util. – Valentin Jun 02 '16 at 20:06
  • not sure if you looked at this https://msdn.microsoft.com/en-us/library/system.windows.gpuinformation%28v=vs.95%29.aspx?f=255&MSPPError=-2147217396 – techspider Jun 02 '16 at 20:07
  • What if you have more than one GPU? – n8wrl Jun 02 '16 at 20:10
  • I can see the 6144MB in (winSat : dedicated video memory) is there a way to use that?Well if you have multiple GPU I just going to assume they have the same amount of memory. – Patrik Fröhler Jun 02 '16 at 20:21

2 Answers2

5

As it is said in the MSDN

Hardware that is not compatible with Windows Display Driver Model (WDDM) returns inaccurate property values for instances of this class.

That is why it returns 4gb.

You can try to use CUDAfy.net

GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
var c = gpu.GetDeviceProperties(true);
var p = c.TotalMemory;
Console.WriteLine(p);
Valentin
  • 5,380
  • 2
  • 24
  • 38
0

There is a wrapper for DirectX called SharpDX. There is a compile-able example in C++ https://code.msdn.microsoft.com/windowsdesktop/DirectX-Video-Memory-ee7d8319 A you could either recreate the code with SharpDX libraries (from the NuGet Package Manager) in visual studio, or you could just compile the C++ code and call the exe as a process from within you c# code.

noone392
  • 1,624
  • 3
  • 18
  • 30