2

I need a multiplatform library to determine how much RAM the OS has free, how much of it is installed, how high the CPU usage is and so on. The library should work in both Windows and POSIX environments. Any suggestions?

Edit: I know it's OS-specific by definition; I can write some small library to abstract this on my own, but I'd rather not do this, if there is already some library available.

Griwes
  • 8,805
  • 2
  • 43
  • 70
  • 2
    I don't think this is available in portable form - this is really OS-specific. – Steve Townsend May 09 '12 at 13:40
  • If you really want something like this, preprocessor directives are probably the way to go. Something like #if (condition that only holds on a Windows) (nextline) /*Code for windows*/ (nextline) #elseif (condition that only holds on POSIX) (nextline) /*Code for POSIX*/ (nextline) #else (nextline) #error Wrong operating System. Or #ifdef. – Whovian May 09 '12 at 13:44
  • 2
    @Whovian, read my edit. I know *how* to do it, if I have to do it on my own; I just ask whether anyone know any such *ready to use* library. – Griwes May 09 '12 at 13:45
  • What is your definition of "free ram"? is it the ram not used by applications? or does that include the ram left after subtracting all the amount of OS caches? Will it include committed but not already populated pages? – PlasmaHH May 09 '12 at 13:45
  • 1
    @PlasmaHH, I mean the physical RAM that OS can allocate to new process. I know that OSes utilize "unused" RAM, but I want it to tell me how much of it is left for applications to use. I don't want to get too much into details of implementation of given OS, just to get an *approximation* of "free" (I think it's still the best word here) RAM in the system. – Griwes May 09 '12 at 13:48
  • The answer about [geekinfo](http://code.google.com/p/geekinfo/) from [this similar question](http://stackoverflow.com/questions/654643/cross-platform-api-for-system-information) might help you – tinman May 09 '12 at 13:56
  • 6
    Isn't it covered by : http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process ? – Joel Falcou May 09 '12 at 13:56
  • You probably won't find a generic POSIX way to do this, it will be very OS specific, like Linux, BSD, etc. Even different versions of Linux may require a different handling. – edA-qa mort-ora-y May 09 '12 at 13:57
  • @JoelFalcou, thanks for linking that question, it will definitely become handy if I'll have to implement this by myself. – Griwes May 09 '12 at 14:05
  • @tinman, geekinfo looks interesting, although it's not quite what I need. – Griwes May 09 '12 at 14:05
  • @SteveTownsend: If you can write multi platform libraries in general, why shouldn't it be possible that such library covers generic runtime information? It is the job of a library to hide platform specifics ... – Sebastian Mach May 14 '12 at 11:00
  • @phresnel - of course it's possible to do this, for example by using macros to switch to platform-specific APIs as suggested above. The point is that there is no single API set that you can call on multiple platforms to get this info, so you are going to have to manage the platform-specific code somehow. My original response said that there was no such library I know of. If that's incorrect - great. – Steve Townsend May 14 '12 at 13:29
  • @SteveTownsend: Looks like I've misread 'available' with 'possible', beg your pardon. – Sebastian Mach May 14 '12 at 13:39

1 Answers1

4

There is almost the same question here, and it has an answer:

https://stackoverflow.com/a/871614/344347

Community
  • 1
  • 1
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127