cudaMemGetInfo Any equivalent for host memory?

I wrote this little function to get some device memory info:

#include <stdio.h>

#include "cuda.h"

void checkGpuMem()


float free_m,total_m,used_m;

size_t free_t,total_t;


free_m =(uint)free_t/1048576.0;



printf ( "  mem free %d .... %f MB mem total %d....%f MB mem used %f MB\n",free_t,free_m,total_t,total_m,used_m);


This was tremendously helpfull. By placing a few calls to checkGpuMem() throughout my program, I was able to pinpoint exactly troubled areas.

I would like to do the same with the host memory but can’t find any suitable function. Is there any? Except using /proc/meminfo maybe.

It turns out that it is quite complicated to get this information in a portable and reliable way. However the hwloc library provides such information : perhaps that’s worth giving a try.

The other alternative might be to use something like valigrind.

Also note that it is certainly impossible to actually know how much memory is currently available for real, except if you have a perfectly dedicated machine (where you would have only a few processes that you totally control). So you can perhaps get the max amount of RAM available, but you certainly can’t get the amount of RAM that you should be able to alloc. Of course, in case you have a dedicated machine, and if you limit yourself to, say, 75% of the memory, it becomes a reasonnable approximation.

Just my 2cents

I just wrote this function:

#include <stdio.h>

#include <unistd.h>

#include "cuda.h"

extern "C"

void checkCpuMem_c()


int page_size;

long int page_total,page_free;

long int memory_total,memory_free;

float free_m,total_m;

page_size =getpagesize();

page_total= sysconf (_SC_PHYS_PAGES);

page_free=sysconf (_SC_AVPHYS_PAGES);



free_m =(uint)memory_free/1048576.0;


printf ( "total memory is %lu  %f  MB free memory is %lu  %f MB  \n",memory_total,total_m,memory_free,free_m);


From some undocumented Linux function.

Problem solved.

It looks like valgrind does not work with CUDA.

It spills a lot of errors which are not there because the test program works fine. It is mainly about malloc function as far as I can tell.

Did anyone had a look at it?

It is difficult to pick the errors from the main C program in that case.

Valgrind works with CUDA. In fact, valgrind with device emulation has, until very recently, been the recommended way of debugging out of bounds memory errors in CUDA.

Just be a piece of code “works fine”, doesn’t mean it doesn’t contain memory management errors.

OK, now I am a little confused. I assumed, based on your original post, that you want to instrument your host memory usage because you have memory leaks or other problems with memory management. That is precisely what valgrind is designed for. Perhaps I misunderstood something, but what is it you actually want to achieve?

You are right, I should have run with emulation code first. I just did with a test code and it works fine in emulation but with the release code, it spits out a lot of errors which are not there.

And yes, I want to get the host part to behave. My checkCpuMem() works well to pinpoints the problem. I am hoping that valgrind can help to do the rest.