Find the answer to your Linux question:
Results 1 to 9 of 9
Hi, I'm looking for a consistent way to determine the available kernel memory. I found the 'free -l' and the cat /proc/meminfo commands, but they don't work on all linux ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5

    Kernel memory determination


    Hi,

    I'm looking for a consistent way to determine the available kernel memory. I found the 'free -l' and the cat /proc/meminfo commands, but they don't work on all linux kernels/distributions. For instance, on the small version of the Wind River Linux distribution, the free command is not available. Is there a consistent method of determining the kernel memory (physically mapped one-to-one) across architectures, distributions, and kernels?

    Additionally, I'd like a global variable that could reliably be used to determine the value. This would be used for OS aware debugging software. If we could look up a symbol to get the value, it would allow us to automatically determine it without requesting input from the user. We could also lookup a routine name that calculates it, execute the routine, and read the value from a register. Any other ideas would also be welcome.

    Thanks,

    Chris

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    Thanks. Unfortunately, it uses meminfo to get all it's information. Also, I need a procedure that I can run on any Linux OS without needing to install new utilities.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    How about the sysinfo() function? This appears to be implemented across architectures/distributions, and I believe the return structure is standard. Does anyone know of any potential pitfalls? For instance, is the structure always fully populated by all distributions?

  6. #5
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    that library claims to run on any 2.2/2.4/2.6 kernel linux distro

    based on the man page for sysinfo, it looks like it has what you need anyway, i would seriously doubt that it is distro dependent, it should only be kernel dependent and work on 2.4/2.6 kernels

  7. #6
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    I agree. Thanks for the input.

    The only thing I worry about is that it won't be included in a distribution. Is that a concern? Is it always included, even on small footprint versions of Linux?

  8. #7
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    seems to be part of libc and glibc, so it should be there, you will probably have to write a small program to make the call though

  9. #8
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    I do have one problem with this method

    there is no way to see cached memory, you can only see free and buffered memory, and since cache memory is only used by disk cache and is technically free, you will be missing this

    Code:
    #include <sys/sysinfo.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        struct sysinfo info;
        int error;
    
        error = sysinfo(&info);
        if (error != 0)
        {
            printf("Error: %d\n", error);
            return error;
        }
    
        printf("Total Memory: %ld\n", info.totalram);
        printf("Free Memory: %ld\n", info.freeram);
        printf("Shared Memory: %ld\n", info.sharedram);
        printf("Buffered Memory: %ld\n", info.bufferram);
        printf("Total High Memory: %ld\n", info.totalhigh);
        printf("Free High Memory: %ld\n", info.freehigh);
        return 0;
    }

  10. #9
    Just Joined!
    Join Date
    Jul 2010
    Posts
    5
    I'm really only interested in low memory utilized in a one-to-one mapping (no virtual use) by the kernel, so the cache memory is not a problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •