开发者

Get total memory of the system with C

I a开发者_Python百科m 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.


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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜