Print GPU Free and Total Memory in Java

This requires jCUDA. At the time of writing, I wasn’t able to install through Maven (with a sbt dependency) but this is actively being looked at1

long total[] = { 0L };
long free[] = { 0L };
 
for (int i = 0; i < deviceCount; i++)
{
  CUdevice device = new CUdevice();
  cuDeviceGet(device, i);
 
  cuInit(0);
  cuDeviceGet(device, 0);
  CUcontext context = new CUcontext();
  cuCtxCreate(context, 0, device);
 
  cuMemGetInfo(total, free);
  System.out.println("Device " + i + ": " + name +
    " memory, total: " + total[0] + ", free: " + free[0]);
}

Want to learn something new? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

Citations:
  1. https://github.com/jcuda/jcuda-main/issues/22 []
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *