PDA

View Full Version : Memory Usage



MK27
07-16-2009, 04:38 PM
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?

bithub
07-16-2009, 05:03 PM
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:


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.

MK27
07-16-2009, 05:52 PM
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:


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.