PDA

View Full Version : Memory & Swap



ghe1
03-19-2002, 11:13 AM
At the prompt, I type "free" which gives me information regarding the total, used, free, cached memory as well as info regarding the swap.

Is there anyway to call "free" from another program and set it up like it is a function call?

Deckard
03-19-2002, 11:34 AM
Free works by simply parsing a dump of /proc/meminfo. You could write a function that reads data in from /proc/meminfo and returns specifics, such as available memory.

Deckard
03-20-2002, 12:04 PM
If you're not interested in parsing a dump from /proc/meminfo, check out sysinfo():


int sysinfo(struct sysinfo *info);

struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
};
sysinfo() is Linux specific, and the struct I included is for kernel versions 2.3.23 and later (assuming your platform is i386). Check the sysinfo(2) man page for details on the sysinfo struct for earlier versions of the kernel.