mert
mert

Reputation: 2002

Get total memory of the system with C

I am trying to get total and if possible free memory of the system by C. It should be system-independent.

To initiate the discussion I can suggest getpagesize() method to get page-size. Anyone can help about number of memory pages would be good.

Upvotes: 1

Views: 1228

Answers (4)

Jens Gustedt
Jens Gustedt

Reputation: 78923

Not only that memory handling is system dependent as others already stated, such a thing as the "total amount of memory" simply doesn't exist as a clear concept in many cases. There is

  • physical memory, but which usually isn't a hard constraint, because it is backed by
  • paged memory on disk, but which configuration dependent and which often isn't a hard constraint either, because of
  • virtual memory, which just describes the different "numbers" that addresses can have and which is freely attributed by the OS, even if it can not be backed by the two above.

Upvotes: 2

Il-Bhima
Il-Bhima

Reputation: 10880

If by "system-independent" you mean OS independent, then I really doubt you'll find any single command to work on all platforms.

If you want system specific:

  • On Windows use GlobalStatusMemoryEx
  • On Linux, I would probably go for the quick and dirty approach (parsing /proc/meminfo)

For Linux you could also try the sysconf function in unistd.h

Upvotes: 1

jweyrich
jweyrich

Reputation: 32240

There is no way of doing this in a system independent fashion. The language has no concept of memory pages, or where and how it's stored.

Upvotes: 3

Martin Beckett
Martin Beckett

Reputation: 96109

There isn't a system independent way of doing this because it is obviously system dependent!

Upvotes: 5

Related Questions