Further memory question [was: Clear the computer's memory?]

Dave Carrigan dave at rudedog.org
Thu Mar 30 16:38:57 UTC 2006


On Thursday 30 March 2006 05:29, R Kimber wrote:

> It's not linux's behaviour that bothers me.  What I'm puzzled about is
> that when I boot up, linux will allocate memory as you describe, and
> Gkrellm will report an amount free. I'm not clear why this free amount
> should change so drastically after some hours of use, when caches etc
> have already been allocated and I'm not starting new apps.  At the
> moment, I still have about 1714MB free, but by tomorrow I'll bet it
> will be down to 700MB.  I don't usually use Firefox, BTW.

Every time something in the system reads data from a file (including 
directories), that data is stored in memory pages in case the system wants to 
re-read the data sometime in the future. It is not released until something 
else needs it. The system doesn't steal memory that's in use by programs to 
store this data, but it will use all of the memory that is otherwise 
available. It will also not release those buffers, even if the program that 
read the files subsequently exits. When a running program decides that it 
needs more memory, the system will happily release some of these buffers so 
that program can have it. The overhead of having to first release a buffer 
before giving the memory to the program is trivial, so there is no downside 
to using otherwise unused memory for buffer cache. 

If you were to do something like "tar cf /dev/null /" just after reboot, you 
would see that your "free" memory quickly gets used up as tar opens and reads 
every file in the filesystem. Even just doing a find from / will do the same 
thing, as every directory is opened and read. 

Overnight, the system runs all kinds of jobs that read from the filesystem, 
such log rotaters that gzip old logs, programs that scan the directory tree, 
etc. All of this stuff will easily result in enough file reads to fill up all 
of the memory that's available for cache, so I would be quite surprised if a 
system that's been up more than a day would have any amount of free memory.

In general, programs that tell you how much free memory you have are mostly 
useless, and you should not spend more than about 1.8 seconds agonizing over 
the fact that the system is using all of the memory you have. It *should* be 
using all of the memory you have. Memory's a precious resource, probably the 
most precious resource on your system. For it not to be used is a huge waste.

If you really want to know more, use the free command in a shell to see how 
much memory is used and how much is used by buffers and cache. This will give 
you a much better idea where your memory is being used.

For example, my system has been up since Sunday at 3:30pm (and I've been 
logged in since that time as well). I'm not having performance problems even 
though I am running firefox and vmware and some openoffice stuff and a bunch 
of other things. Here is what free reports:

             total       used       free     shared    buffers     cached
Mem:       2073996    2037700      36296          0     152664     504520
-/+ buffers/cache:    1380516     693480
Swap:      4194296    1615072    2579224

As you can see, I have pretty much no free memory (36K out of 2G). But, 693K 
of that is in buffers and is immediately available to any program that wants 
it with no impact on those programs' performance. In the meantime, that 
memory contains data from files that, statistically, the system is most 
likely to need to read in the near future. The cost savings of not having to 
re-read it from disk will be immense. That is a good thing.

You'll also see that I'm using 1.6 G of swap. This is not necessarily a bad 
thing. There are probably quite a few programs that have been launched that I 
haven't switched to for a while. Some other program needed memory, and the 
system decided to swap one of those inactive programs out to the the swap 
partition so that it could give the other program the memory. The only time I 
will notice this is when I want to use that other program - there will be a 
noticeable delay while that program's memory gets restored from swap. Still, 
this is not necessarily bad. The only time it gets bad is if you find that 
every time you switch to a program, the system needs to swap something else 
out and swap that program back in. 

If you are concerned about swapping, use the vmstat command with an interval 
of 30 seconds or so and watch the output for a while. The only time to get 
worried is if the "so" column is regularly non-zero. That is a sign that your 
system regularly has to swap programs out, and it means you could benifit 
from more adding more RAM. Of course, if that's happening, the free command 
would also be reporting that there is very little memory being used by the 
buffer cache as well, which would also impact performance.

As to the people that advocate running without swap, I think that's bad 
advice. Swapping per se isn't necessarily bad, and the alternative of having 
a program crash because it can't get enough memory is much worse IMO.

-- 
Dave Carrigan
Seattle, WA, USA
dave at rudedog.org | http://www.rudedog.org/
UNIX-Apache-Perl-Linux-Firewalls-LDAP-C-C++-DNS-PalmOS-PostgreSQL-MySQL-Postfix
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20060330/69ef5062/attachment.sig>


More information about the ubuntu-users mailing list