Determining memory availabilty.

Jim Barton jmb at patton.wpd.sgi.com
Sun Feb 24 07:13:37 AEST 1991


In article <9102211011.AA03206 at crow.omni.co>, rpaul at crow.UUCP (Rodian Paul) writes: 
|> How can I determine at run time how much virtual memory (RAM + swap)
|> is available prior to my calling malloc(3C) or malloc(3X)?
|> 
|> Perhaps I want to warn the user that this application ain't gunna run too
|> fast 'cos it'll hit swap and suggest the user get rid of some of those 
|> processes they've got cluttering up the console.
|> 
|> Perhaps I want to make sure that the amount of memory I need is resident
|> on this machine. I.e with 32MB RAM and 50MB swap, I can still have
|> a valid pointer to 511 MB returned from malloc(3C), or 289 MB from malloc(3X).
|> 
|> Is there a simple way to do this? How does osview/gr_osview handle it?
|> 
|> Suggestions/tips will be appreciated.
|> -------------------------------------------------------------------------------
|> crow!rpaul at ccut.cc.u-tokyo.ac.jp	phone: +81 (3) 5706-8357
|> ccut.cc.u-tokyo.ac.jp!crow!rpaul	  FAX: +81 (3) 5706-8437

There are two obvious ways to do this.

First, use the sysmp(2) system call to fetch the minfo structure (described in /usr/include/sys/sysinfo.h), which gives the amount of free swap available at any given point in time, and use the MPKA_FREEMEM option to get the free real memory available. Then do the obvious calculation.

The second, possibly simpler way is to use the statfs(2) system call on the /debug file system. /debug overall filesystem size is the same as the total amount of virtual memory available, free space is the same as the amount of free virtual space available. The inode count fields give the total number of process slots, and number free.

-- Jim Barton
   Silicon Graphics Computer Systems
   jmb at sgi.com



More information about the Comp.sys.sgi mailing list