views:

301

answers:

4

Linux noob question:

If I have 500MB of RAM, and 500MB of swap space, can the OS and processes then use 1GB of memory?

In other words, is the total amount of memory available to programs and the OS the total of the physical memory size and swap size?

I'm trying to figure out which SNMP counters to query, but need to understand how Linux uses virtual memory a little better first.

Thanks

A: 

Yes, this is essentially correct. The actual numbers might be (very) marginally lower, but for all intents and purposes, if you have x physical memory and y virtual memory (swap in linux), then you have x + y memory available to the operating system and any programs running underneath the OS.

Matthew Scharley
+1  A: 

Although mostly it's true, it's not entirely correct. For a particular process, the environment you run it in may limit the memory available to your process. Check the output of ulimit -v as well.

Pavel Shved
The question stated if the "OS and the processes" could use a certain amount of memory. I think this answer is somewhat misleading, as it addresses a different question, namely if "a particular process" can use a certain amount of memory.
vstrien
@vstrien , FINE, I've fixed my question for those who doesn't *entirely* understand the word "*entirely*"
Pavel Shved
+1  A: 

Nothing is ever quite so simple anymore...

Memory pages are lazily allocated. A process can malloc() a large quantity of memory and never use it. So on your 500MB_RAM + 500MB_SWAP system, I could -- at least in theory -- allocate 2 gig of memory off the heap and things will run merrily along until I try to use too much of that memory. (At which point whatever process couldn't acquire more memory pages gets nuked. Hopefully it's my process. But not always.)

Individual processes may be limited to 4 gig as a hard address limitation on 32-bit systems. Even when you have more than 4 gig of RAM on the machine and you're using that bizarre segmented 36-bit atrocity from hell addressing scheme, individual processes are still limited to only 4 gigs. Some of that 4 gigs has to go for shared libraries and program code. So yer down to 2-3 gigs of stack+heap as an ADDRESSING limitation.

You can mmap files in, effectively giving you more memory. It basically acts as extra swap. I.e. Rather than loading a program's binary code data into memory and then swapping it out to the swapfile, the file is just mmapped. As needed, pages are swapped into RAM directly from the file.

You can get into some interesting stuff with sparse data and mmapped sparse files. I've seen X-windows claim enormous memory usage when in fact it was only using up a tiny bit.

BTW: "free" might help you. As might "cat /proc/meminfo" or the Vm lines in /proc/$PID/status. (Especially VmData and VmStk.) Or perhaps "ps up $PID"

Mr.Ree
+4  A: 

Actually, it IS essentially correct, but your "virtual" memory does NOT reside beside your "physical memory" (as Matthew Scharley stated).

Your "virtual memory" is an abstraction layer covering both "physical" (as in RAM) and "swap" (as in hard-disk, which is of course as much physical as RAM is) memory.

Virtual memory is in essention an abstraction layer. Your program always addresses a "virtual" address, which your OS translates to an address in RAM or on disk (which needs to be loaded to RAM first) depending on where the data resides. So your program never has to worry about lack of memory.

vstrien
I never said anything was beside anything, I just stated that the amount of space available was the combination of the two. But oh well.
Matthew Scharley
In your post I read "if you have x physical memory and y virtual memory (swap in linux), then you have x + y memory available". However, virtual memory isn't swap, virtual memory is the abstraction layer covering both swap and "real" internal memory.
vstrien
The line is blurred a bit. For instance, in Windows, when you configure "virtual memory", you're configuring the same thing as swap in Linux. You don't configure how much total memory you want, you configure how much of your hard disk is used for paging.
Matthew Scharley
a) would be nice if you notified in comments if you think you found something wrong in my answer b) there actually is nothing wrong in my one, if you read it better.
Pavel Shved
vstrien