Thread: Memory & Swap

  1. #1
    Registered User
    Join Date
    Feb 2002

    Memory & Swap

    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?

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    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.
    Jason Deckard

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    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.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM