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]);
}
  1. https://github.com/jcuda/jcuda-main/issues/22 []

Leave a Reply

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