Thread: Memory Usage

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Memory Usage

    So I am getting a low cost unmanaged "Virtual Private Server", which means just a root access OS with limited resources. It hasn't been set up yet, but there is only 128mb of ram, which I figured ain't very much so I was just looking at top and trying to decide what number applies here. I'm sure it's enough to run the kernel & OS (debian 5), but apache is kind of a wild card.

    ps -o vsz I've never used before and it seems to have it's output truncated at 6 digits which is supposedly the number of bytes of virtual memory used. This number does not even remotely correspond to the values in top, which is all I normally bother with because mem usage is never an issue for me. So almost all of it is firefox and X, and a passive apache has a very small "residential" size but a big "virtual" size, like 260mb. That can't be right.

    Does anyone know what I should be looking at here?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well I'm guessing that the 128MB is physical memory allotted to you. This has no bearing on the amount of virtual memory your applications can use though. Apache probably allocates a bunch of memory up front, but uses very little of it. That is why it is using a bunch of virtual memory, but little physical memory.

    Remember that in Linux, malloc() is always guaranteed to return non-NULL as long as you have the virtual address space available. In other words, you can do this:
    Code:
    int main(void)
    {
        unsigned char* mem_hog = malloc( 1GB );
        // Linux will always return a valid pointer, even if you don't have 1GB of RAM available.  After
        // the above call, your application will be using 1GB of virtual memory, but very little physical 
        // memory.
      
        memset(mem_hog, 0, 1GB );
        // The call to memset() will now require you to have 1GB of memory available.  That means
        // your available RAM plus your available swap space needs to be at least 1GB.  If you look
        // at memory usage after this call, your physical memory usage will jump way up.  It may not
        // equal 1GB though, because chances are that some of that 1GB will be swapped out to 
        // disk.   
    }
    The moral of this story is that it is very hard to track how much physical RAM is available on a Linux machine. Any RAM that is not being used by applications will probably get used by the kernel to cache I/O requests to disks. I'm not sure how this is managed by a virtual private server though.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    Apache probably allocates a bunch of memory up front, but uses very little of it. That is why it is using a bunch of virtual memory, but little physical memory.

    Remember that in Linux, malloc() is always guaranteed to return non-NULL as long as you have the virtual address space available. In other words, you can do this:
    Code:
    int main(void)
    {
        unsigned char* mem_hog = malloc( 1GB );
        // Linux will always return a valid pointer, even if you don't have 1GB of RAM available.  After
        // the above call, your application will be using 1GB of virtual memory, but very little physical 
        // memory.
    Okay, so I'm guessing apache could even use a system call like "free" to get the amount of available memory and then claim a percentage of it, because the initial amount of residential memory is 4mb.

    "Passenger", the apache module which runs ruby on rails, is about 7mb virtual and residential at start up. There is no mention of mod_perl (in top), but after I load a page requiring it new httpd processes taking 16mb virtual and residential appear. That would make sense, supposedly mod_perl is a memory hog and so it's hard to find hosting for it, which is why I'm trying VPS. It's almost exciting.

    But that's more like what I would expect, the OS + server taking <32 mb at start-up. Thanks for the illustration, bithub, that's quite a potential discrepancy.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. how much memory usage for my program?
    By elninio in forum Linux Programming
    Replies: 4
    Last Post: 08-01-2008, 03:58 PM
  4. Structs vs. Classes, Memory Usage
    By CrazyNorman in forum Game Programming
    Replies: 2
    Last Post: 07-17-2005, 05:43 PM
  5. Memory Usage
    By ghe1 in forum Linux Programming
    Replies: 0
    Last Post: 03-18-2002, 09:43 AM