[ale] Swap Memory usage

Joe Knapka jknapka at kneuro.net
Sat Sep 20 18:51:54 EDT 2003


David Corbin <dcorbin at machturtle.com> writes:

> Why does Linux use swap memory when there is RAM available?

It depends on a lot of variables, and I haven't looked at
the MM code in some time, but at least the following
may affect this behavior:

(1) Reserved RAM. The system always tries to keep a certain amount of
RAM free. The reason for this is that in many cases, swapping out RAM
to free up memory for some immediate need actually requires some RAM
to be allocated in order to bootstrap the swap-out process. The
reserved amount is quite small, but I don't remember the exact
value. The kernel will swap stuff out in preference to having too few
free pages.

(2) Historic RAM usage. If in the past the kernel had to swap a bunch
of stuff out to make way for some memory hog, that stuff will just
stay swapped out until the kernel needs it again. No point in hitting
the disk for data you may not actually use.

(3) DMA and other memory-range constraints. Only certain ranges of
physical RAM are appropriate for DMA activity (that is, "Direct Memory
Access", which permits devices to write data directly to RAM without
involving the CPU). If some process is using a bunch of DMA pages,
those pages might get swapped out by the kernel because it needs them
to fulfill an I/O request. Similarly, if you have >1GB of physical
RAM, most kernel data structures must live in the low 1GB. So if a
process has data pages living in the low 1GB, they may get evicted to
swap so the kernel can use the resultant free RAM.

(4) Overcommit disabled. By default, the Linux kernel only allocates
swap space when it really needs to swap something out. The result is
that it's possible to (logically) allocate more memory then the total
of (physical RAM + available swap), which is usually fine, becuase
it's rare for a process to actually use every bit of RAM it allocates
(think of sparse matrices). This is called "VM overcommit", and it's
usually a good thing. However, sometimes it's really, really bad, and
it's possible to disable overcommit using some /proc entry (I don't
recall the exact name). If that's done, then the kernel will allocate
(though not necessarily write out to disk) a page of swap space for
every page of anonymous memory allocated, and those allocated entries
would very likely show up as "swap space in use" (though I don't know
for sure).

(5) Weird kernel versions. If you're using one of the many different
*-mm-* kernel versions supplied by different folks (Andrea Archangeli,
Rik van Riel, etc.), they may all behave somewhat differently in this
regard.

Cheers,

-- Joe Knapka

> David
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list