how to determine amount of free memory

how to determine amount of free memory

Post by Tim Davenpo » Fri, 29 Nov 2002 06:28:23



I'm writing a C program that tracks statistics about
a running UNIX system.  One statistic I would like
the program to track is the amount of memory that is
available to be allocated.

Can anyone describe how that could be determined?

 
 
 

how to determine amount of free memory

Post by Eric Sosma » Fri, 29 Nov 2002 07:13:44



> I'm writing a C program that tracks statistics about
> a running UNIX system.  One statistic I would like
> the program to track is the amount of memory that is
> available to be allocated.

> Can anyone describe how that could be determined?

    df -k /tmp

... is a pretty reasonable estimate on most Unices.

--


 
 
 

how to determine amount of free memory

Post by Maurizio Loret » Fri, 29 Nov 2002 07:01:01



> I'm writing a C program that tracks statistics about
> a running UNIX system.  One statistic I would like
> the program to track is the amount of memory that is
> available to be allocated.

> Can anyone describe how that could be determined?

Time to post that again ...  Works on Linux, Digital Unix and Solaris.
Have fun.

--------------------Begin file: memory.c
/*------------------------------------------------------*
 | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO      |
 | Work:   University of Padova - Department of Physics |
 |         Via F. Marzolo, 8 - 35131 PADOVA - Italy     |
 | Phone:  ++39(49) 827-7216     FAX: ++39(49) 827-7102 |

 | WWW:    http://wwwcdf.pd.infn.it/~loreti/mlo.html    |
 *------------------------------------------------------*

 Tries to list available memory on several architectures;
 Needs -lmach to be linked under Digital Unix.

 *------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>

static int memory(void);

int main()
{
  struct utsname un;

  if (uname(&un) >= 0) {
    fprintf(stdout, "RAM on machine \"%s\", running %s %s :\n",
            un.nodename, un.sysname, un.release);
  }
  return memory();

Quote:}

#ifdef __sun__
static int memory(void)
{
  long npag;                    /* Number of physical pages */
  long nbyt;                    /* Numper of bytes per page */
  int nmeg;                     /* Number of Megabytes on the system */
  double x;

  npag = sysconf(_SC_PHYS_PAGES);
  nbyt = sysconf(_SC_PAGESIZE);
  x = npag;
  x *= nbyt;
  x /= 1024.0;
  x /= 1024.0;
  nmeg = x + 0.5;
  fprintf(stdout,
          "%ld Pages, %ld Bytes each - %d MBytes total.\n",
          npag, nbyt, nmeg);

  return EXIT_SUCCESS;

Quote:}

#endif /* __sun__ */

#ifdef __linux__
#include <ctype.h>
#include <fcntl.h>
#define BUFFER_LEN 128

static int memory(void)
{
  int retVal = EXIT_FAILURE;
  int fd;

  if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) {
    fputs("Couldn't open /proc/meminfo\n", stderr);
    perror("open");
  } else {
    char buffer[BUFFER_LEN];
    size_t len;

    if ((len = read(fd, buffer, sizeof(buffer)-1)) < 0) {
      fputs("Error read from /proc/meminfo\n", stderr);
      perror("read");
    } else {
      const char *pc = buffer;
      char *end;
      unsigned long total, used, free;

/* buffer[len]='\0'; puts(buffer); */

      while (*pc++ != '\n') {}
      while (!isspace((unsigned char) *pc)) pc++;
      total = strtoul(pc, &end, 0) / 1024;
      used = strtoul(end, &end, 0) / 1024;
      free = strtoul(end, &end, 0) / 1024;
      fprintf(stdout,
              "Total memory: %lu kBytes (%lu kBytes used, %lu kBytes free)\n",
              total, used, free);
      retVal = EXIT_SUCCESS;
    }
    close(fd);
  }
  return retVal;

Quote:}

#endif /* __linux__ */

#ifdef __osf__
#include <mach.h>
#include <mach/vm_statistics.h>
#define LOG1024 10              /* log base 2 of 1024 */

static int memory(void)
{
  int pagesize;
  int pageshift;                /* log base 2 of the pagesize */
  long active, total, free;
  vm_statistics_data_t vmstats;

  pagesize = getpagesize();
  for (pageshift = 0;   pagesize > 1;   pageshift++,  pagesize >>= 1) {}
  pageshift -= LOG1024;

  (void) vm_statistics(task_self(), &vmstats);
  total = active = vmstats.active_count << pageshift;
  total += (free = vmstats.free_count << pageshift);
  total += (vmstats.inactive_count + vmstats.wire_count) << pageshift;
  fprintf(stdout,
          "%ld active, %ld free, %ld total (in kBytes).\n",
          active, free, total);
  return EXIT_SUCCESS;

Quote:}

#endif /* __osf__ */

--
Maurizio Loreti                         http://www.pd.infn.it/~loreti/mlo.html

 
 
 

how to determine amount of free memory

Post by Ivan Rayne » Fri, 29 Nov 2002 09:15:46




>> I'm writing a C program that tracks statistics about
>> a running UNIX system.  One statistic I would like
>> the program to track is the amount of memory that is
>> available to be allocated.

>> Can anyone describe how that could be determined?

> Time to post that again ...  Works on Linux, Digital Unix and Solaris.
> Have fun.

Not that anyone would care :), but the IRIX version would be something like:

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/sysmp.h>

/*
 * Print some memory stats in kilobytes.
 */
int
main()
{
        struct rminfo rminfo;
        uint32_t page_sz;

        if (sysmp(MP_SAGET, MPSA_RMINFO, &rminfo, sizeof(rminfo))) {
                fprintf(stderr, "sysmp failed: %s\n", strerror(errno));
                return errno;
        }

        page_sz = getpagesize();

        printf("freemem   %u\n", rminfo.freemem   * (page_sz / 1024));
        printf("availsmem %u\n", rminfo.availsmem * (page_sz / 1024));
        printf("availrmem %u\n", rminfo.availrmem * (page_sz / 1024));

        return 0;

Quote:}

See /usr/include/sys/sysmp.h for comments on the rminfo structure.

Ivan
--
Ivan Rayner

 
 
 

how to determine amount of free memory

Post by Tim Davenpo » Sat, 14 Dec 2002 06:21:56


Thanks!  This was exactly what I was looking for.

Would anyone happen to have a FreeBSD version?



> > I'm writing a C program that tracks statistics about
> > a running UNIX system.  One statistic I would like
> > the program to track is the amount of memory that is
> > available to be allocated.

> > Can anyone describe how that could be determined?

> Time to post that again ...  Works on Linux, Digital Unix and Solaris.
> Have fun.

> --------------------Begin file: memory.c
> /*------------------------------------------------------*
>  | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO      |
>  | Work:   University of Padova - Department of Physics |
>  |         Via F. Marzolo, 8 - 35131 PADOVA - Italy     |
>  | Phone:  ++39(49) 827-7216     FAX: ++39(49) 827-7102 |

>  | WWW:    http://wwwcdf.pd.infn.it/~loreti/mlo.html    |
>  *------------------------------------------------------*

>  Tries to list available memory on several architectures;
>  Needs -lmach to be linked under Digital Unix.

>  *------------------------------------------------------*/

> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/utsname.h>

> static int memory(void);

> int main()
> {
>   struct utsname un;

>   if (uname(&un) >= 0) {
>     fprintf(stdout, "RAM on machine \"%s\", running %s %s :\n",
>        un.nodename, un.sysname, un.release);
>   }
>  return memory();
> }

> #ifdef __sun__
> static int memory(void)
> {
>   long npag;                    /* Number of physical pages */
>   long nbyt;                    /* Numper of bytes per page */
>   int nmeg;                     /* Number of Megabytes on the system */
>   double x;

>   npag = sysconf(_SC_PHYS_PAGES);
>   nbyt = sysconf(_SC_PAGESIZE);
>   x = npag;
>   x *= nbyt;
>   x /= 1024.0;
>   x /= 1024.0;
>   nmeg = x + 0.5;
>   fprintf(stdout,
>           "%ld Pages, %ld Bytes each - %d MBytes total.\n",
>           npag, nbyt, nmeg);

>   return EXIT_SUCCESS;
> }
> #endif /* __sun__ */

> #ifdef __linux__
> #include <ctype.h>
> #include <fcntl.h>
> #define BUFFER_LEN 128

> static int memory(void)
> {
>   int retVal = EXIT_FAILURE;
>   int fd;

>   if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) {
>     fputs("Couldn't open /proc/meminfo\n", stderr);
>     perror("open");
>   } else {
>     char buffer[BUFFER_LEN];
>     size_t len;

>     if ((len = read(fd, buffer, sizeof(buffer)-1)) < 0) {
>       fputs("Error read from /proc/meminfo\n", stderr);
>       perror("read");
>     } else {
>       const char *pc = buffer;
>       char *end;
>       unsigned long total, used, free;

> /* buffer[len]='\0'; puts(buffer); */

>       while (*pc++ != '\n') {}
>       while (!isspace((unsigned char) *pc)) pc++;
>       total = strtoul(pc, &end, 0) / 1024;
>       used = strtoul(end, &end, 0) / 1024;
>       free = strtoul(end, &end, 0) / 1024;
>       fprintf(stdout,
>               "Total memory: %lu kBytes (%lu kBytes used, %lu kBytes free)\n",
>               total, used, free);
>       retVal = EXIT_SUCCESS;
>     }
>  close(fd);
>   }
>  return retVal;
> }
> #endif /* __linux__ */

> #ifdef __osf__
> #include <mach.h>
> #include <mach/vm_statistics.h>
> #define LOG1024 10              /* log base 2 of 1024 */

> static int memory(void)
> {
>   int pagesize;
>   int pageshift;                /* log base 2 of the pagesize */
>   long active, total, free;
>   vm_statistics_data_t vmstats;

>   pagesize = getpagesize();
>   for (pageshift = 0;   pagesize > 1;   pageshift++,  pagesize >>= 1) {}
>   pageshift -= LOG1024;

>   (void) vm_statistics(task_self(), &vmstats);
>   total = active = vmstats.active_count << pageshift;
>   total += (free = vmstats.free_count << pageshift);
>   total += (vmstats.inactive_count + vmstats.wire_count) << pageshift;
>   fprintf(stdout,
>           "%ld active, %ld free, %ld total (in kBytes).\n",
>           active, free, total);
>   return EXIT_SUCCESS;
> }
> #endif /* __osf__ */