How to retrieve total amount of system RAM?

Benji Smith dlanguage at benjismith.net
Wed Aug 13 12:58:05 PDT 2008


Steven Schveighoffer wrote:
> Getting system memory isn't part of any standard that I know of (at least on 
> the posix side), and there's not a large demand for it.  The API for getting 
> memory is decided on by OS implementors.  What you would end up with is a 
> list of versions for not only different OSes, but different versions of the 
> OS.  It's along the same lines as getting/setting network interface 
> addresses.
> 
> From experience (I have written a program to get system memory size), it is 
> not trivial.  And not always accurate.
> 
> This is not to say that it couldn't be added.  If you come up with something 
> that looks reasonable, Tango might accept it.
> 
> -Steve 


In win32, it looks like the GlobalMemoryStatus (for all non-NT versions) 
and GlobalMemoryStatusEx (for all NT-based versions), from kernel32.dll, 
would do the trick.

http://msdn.microsoft.com/en-us/library/aa366586.aspx
http://msdn.microsoft.com/en-us/library/aa366589(VS.85).aspx

In linux, a bare-minimum solution would be to read from /proc/meminfo, 
even if there's no POSIX function.

Of course, I'm talking out of my ass here, because my experience with 
C/C++ programming is very thin. I don't know, for example, how you'd 
check for the existence of the GlobalMemoryStatusEx function and fall 
back to GlobalMemoryStatus when it doesn't exist.

It seems doable, though my own systems programming experience is 
probably too lacking for me to make it happen myself.

--benji


More information about the Digitalmars-d-learn mailing list