Re: Querying Kernel about RAM

From: Devlin Doe (devlin_at__NIXPIX_piratefish.net)
Date: 09/29/04


Date: Wed, 29 Sep 2004 22:34:14 +0200

On Tue, 28 Sep 2004 18:42:25 +0200, James Dabbs wrote:

> Is there a way for a utility application to query the kernel regarding
> the amount of physical ram in the box as well as the amount of RAM that
> is recognized by the kernel?
>
> For instance, if I have a machine with 1G and I pass a mem=512K param to
> the kernel, I'd like a way for my application to figure out what is
> going on.
>

As previous posters have said there are several methods of getting the
memory size:

 * cat /proc/meminfo
   - Highly linux dependant, including kernel version
 * sysinfo()
   - Never used it but it seems to give the same info as /proc/meminfo
 * sysconf(_SC_PAGESIZE)*sysconf(_SC_PHYS_PAGES)
   - The portable way.

See example code below for test usage, I have no idea how the linux
mem parameter is handled..

Regards
Devlin

---8<----8<-----chop-with-scissors----
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysinfo.h>

int main(){
  struct sysinfo si;
  long page_size, num_pages;
  
  page_size = sysconf(_SC_PAGESIZE);
  num_pages = sysconf(_SC_PHYS_PAGES);
  printf("sysconf: Total ram %ld MB\n", (page_size*num_pages)/(1024*1024));
   
   memset(&si, 0, sizeof(sysinfo));
   if ( sysinfo(&si) == 0 ){
      printf("sysinfo: Total ram %ld MB\n", si.totalram/(1024*1024));
   }
   return 0;
}
--->8---->8-----end-chop



Relevant Pages

  • Re: Querying Kernel about RAM
    ... > Is there a way for a utility application to query the kernel regarding the ... > amount of physical ram in the box as well as the amount of RAM that is ... VmallocChunk: 501276 kB ...
    (comp.os.linux.development.system)
  • Re: Querying Kernel about RAM
    ... > Is there a way for a utility application to query the kernel regarding the ... > amount of physical ram in the box as well as the amount of RAM that is ...
    (comp.os.linux.development.system)
  • Querying Kernel about RAM
    ... Is there a way for a utility application to query the kernel regarding the ... amount of physical ram in the box as well as the amount of RAM that is ...
    (comp.os.linux.development.system)
  • Re: A couple of questions about slabifo
    ... Is there any method to find, from the user space, the amount of ... physical RAM that kernel uses? ...
    (comp.os.linux.development.system)
  • [PATCH 09/14] x86, boot: add new runtime_address and runtime_size bzImage fields
    ... Make the runtime address and the required amount of linear ... can use this information to select a kernel location; ... leal relocated, %eax ... -/* Find the address of the relocations. ...
    (Linux-Kernel)