LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   test how much mem the operating system has free in C (https://www.linuxquestions.org/questions/programming-9/test-how-much-mem-the-operating-system-has-free-in-c-762832/)

smeezekitty 10-18-2009 04:24 PM

test how much mem the operating system has free in C
 
with GNU CC how do i test how much memory is free to the entire operating system ?
old turbo C++ has coreleft() but GCC does not have it

verdeboy2k 10-18-2009 05:05 PM

sysinfo() can do that:

Code:

#include <sys/sysinfo.h>

int main() {
    struct sysinfo info;

    sysinfo(&info);

    printf("RAM: %d\n",info.totalram);

    return 0;
}

For more information:
Code:

man sysinfo
This is linux specific, I don't know how to do this on other platforms. There is no GCC or ISO C specific call for this that I know of.

smeezekitty 10-18-2009 05:51 PM

need cross platform

nadroj 10-18-2009 06:16 PM

Quote:

Originally Posted by smeezekitty (Post 3724215)
need cross platform

a "thanks for trying" would have been nice.

as verdeboy2k said, and i agree with, i dont think there is a cross-platform GNU C API to do this, as it relies heavily on the OS.

smeezekitty 10-18-2009 06:17 PM

how about a library?

nadroj 10-18-2009 06:21 PM

there are a number of ways to do it in linux, one of them mentioned above, and a simple search will give you the windows API to do it. i dont know of any library that wraps them all to make it seem cross-platform

smeezekitty 10-18-2009 06:23 PM

ok then, i will write a library
one question is -- what windows API will give you the same info

nadroj 10-18-2009 06:32 PM

regarding the library, a search gave a forum which referenced this: http://code.google.com/p/geekinfo/. the description seems to be what you want, though i didnt look at any other details.

i dont remember the exact windows API one (im sure after some searching MSDN you will find it), but a good starting point is here: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx


All times are GMT -5. The time now is 03:34 AM.