memory cache timeout

James Wilkinson james at westexe.demon.co.uk
Sat Sep 18 09:29:06 UTC 2004


Douglas Phillipson wrote:
> Is there a system setting for how long memory is cached after an 
> application releases it?

Errm: I'm not sure what you mean here. But if you mean what I think
you mean, the answer is no. But there is a related setting you can
tweak.

You can divide the memory a process uses into "anonymous" working
memory, and that which consists of pages [1] from disk that have been
paged into memory.

Anonymous memory has no meaning after a process releases it, and is
returned to the store of truly free memory.

Memory-mapped pages still contain a valid copy of pages on disk that
might be used again, and are considered as part of disk cache.

It is generally agreed that it is better for otherwise unused memory
to be used for disk cache: the pages that are being cached *might* be
wanted again in the future, in which case they will speed things up,
while completely unused memory is not doing anything for you. So disk
cache is kept as disk cache until it is otherwise needed.

There are a few pages kept completely unused so that when processes
need new memory, they don't have to wait for pages to be freed.

When the system gets low on free memory, it looks around for pages that
haven't been touched for a while. Anonymous memory gets written to
paging space (the swap file or partition), while memory mapped pages,
if they've changed, get written back to the file they came from [2].
Then the page is considered free: this is called reclaiming memory.

This is all true for practically all Unixes with memory management
(the big difference is exactly what chooses the pages to be written
to disk: whether it's the kernel, a kernel-mode process, or a
privileged user-space process). Other OSes with memory management
are similar.

On Linux, there is the setting /proc/vm/sys/swappiness. It controls how
likely the kernel is to reclaim program (both anonymous and memory
mapped) memory, and how likely it is to reclaim disk cache that *isn't*
used by a process. Set it to 0 and Linux will normally just reclaim
disk cache. Set it to 100 (the maximum setting) and Linux will consider
either sort of page. Set it somewhere in between, and results will vary
depending on how difficult Linux is finding it to reclaim pages and how
much memory is being used by programs.

See http://lwn.net/Articles/83588/ for more details.

Hope this helps!

James.

[1] For memory management purposes, memory, paging space, and a
processes view of memory are all divided into pages. Normally, on
Power, i86 and x86-64, a page is 4 K.

[2] If they haven't changed from what's on disk, you've already got a
copy of the data on disk!

-- 
E-mail address: james | Banana in disk drive error
@westexe.demon.co.uk  | 





More information about the users mailing list