APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Where's the memory?

© June 2006 Anthony Lawrence

I had happened across How To Remotely Monitor Memory Usage and smiled when I saw this :

As one example of this phenomenon, the SUSE Linux system that I use for my public Web and e-mail server originally only had 512 MB of RAM. This should have been more than adequate for the lightweight load that I was expecting, but the system tools showed that the box was constantly running low on memory. And even after I bumped it up to a full gigabyte, the system still complained.

I had just gotten ready to add another gigabyte when I discovered that most of the memory was actually being consumed by SUSE's aggressive disk-caching algorithm. Every time I added more memory, the operating system drank it up for the disk cache. In reality I was nowhere near hitting any kind of memory limit for the applications themselves, even with just 512 MB.

"That's how Linux memory works", I thought to myself, "he should have known that."

Well, yeah, he probably should have. That once again points out the danger of assuming you know what's going on based upon your knowledge of other systems. Being an expert at Solaris is helpful with Linux but it can also lead you astray. The opposite is just as true, of course.

But knowing something about "Linux" in general can also send you down the wrong path. I said that's how Linux memory works, but that isn't necessarily true either. The same article goes on to say:

RedHat and SUSE have substantially different algorithms when it comes to allocating cache memory. Where SUSE is very aggressive and tries to make full use of the available memory, RedHat leaves unused memory lying around in case it's suddenly needed by something.

So if you've "learned" that Linux uses available memory for buffer caching, a RedHat system that doesn't could confuse you. But wait - that's not all.

"Swappiness" was introduced in 2.6 kernels. When some program wants more ram and the kernel has a bunch being used for caching, it could give up some of that. Or it could just make some other less active program swap out to disk. Which is a better idea? Well, of course that depends on what is going on right now. If there is heavy disk activity and programs that really aren't being used, it would make sense to swap. If there isn't much disk activity, or those swappable programs are apt to become active very soon, it makes sense to give back memory.

The idea was that the admin knows best what his system might require, so the admin should be able to say how much "swappiness" is desirable. If swappiness was set to zero (echo 0 >/proc/sys/vm/swappiness), the kernel would give up cache to anything that wanted ram; if it were set to 100 it would do the opposite and let the processes swap.

Well, great, but what if you don't have a clue because sometimes your system has unused apps and sometimes it doesn't.? No problem, somebody came up with something that would make a good guess on the fly: Autoregulated VM Swappiness Patch.

"[The] amount of swap space consumed is also taken into account, and the size of it compared to the physical ram is taken into consideration when making its effect on the value of swappiness. With this patch, this should make any machine that has swapspace as resistant to OOM as possible. This version by default autoregulates the swappiness, but also allows you to choose a manual setting if you so desire by echo 0 > /proc/sys/vm/autoswappiness and then setting the swappiness the manual way as previously. This makes comparison with autoregulation easy."

But then: ( Bugzilla Bug 54560):

As of 2.6.7, gentoo-dev-sources no longer contains the Con Kolivas autoregulated swappiness patch. This is important for desktop machines, to avoid having large amounts of memory swapped out on prolonged system inactivity (causing an annoying swapstorm when the machine is used again).

Desktops and laptops are apt to sit unused for long periods. Therefore unused processes tend to get swapped. If the kernel were really smart, it would notice that *nothing* of importance is happening and just leave things as is, but apparently we haven't reached that point yet. Or if we could have so much real physical ram that we could eliminate swapping entirely..

Well, the minute you say "eliminate swap", someone will point out that you *need* swap. There's a long discussion of swap and ram at Kernel trap: Is Swap Necessary?, but most of the conversation there is at cross-purposes. For multi-user machines, you definitely want swap because you want to be able to be truly interactive: moving one program aside so that another can run. But for a single user machine, you may not want to swap: it might be preferable to have the system refuse to start up a new app and let you decide what you'd rather get rid of to make room for it.

Or maybe not. I'm in favor of control, but the default probably should be to swap. Then again, maybe the default for desktops and laptops should be different.. it remains a complicated and difficult subject.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Where's the memory?

1 comment

Inexpensive and informative Apple related e-books:

iOS 10: A Take Control Crash Course

Take Control of High Sierra

Take Control of Parallels Desktop 12

Take Control of iCloud

Photos: A Take Control Crash Course

More Articles by © Anthony Lawrence

Sat Jun 17 22:31:58 2006: 2126   drag

Ya.. this was a huge deal for a long time. People bitching about how when they would come back to their machines after a few hours of inactivity would watch the machine chug a bit as they openned up Mozilla and everything came back to life.

One of the deals is, however, that as your gone the machine still does have activity. Not a big deal, but you have your hourly and daily cron job. For instance you run updatedb which builds your man file database and 'locate' command database and such. So there is always something going on.

So I think that 'swapiness' was a sort of a comprimize. People would complain and kernel developers would say something along the lines of "What you want isn't going to do what you think it will do and your not going to like it". But even then it makes sense to regulate it somewhat so users could set the swapiness to 'unswap' or whatever and find out for themselves.

I learned memory management the 'wrong way' with messing around with Windows 95 and 98. I had to arrange it so that as much memory was free so it could be used for my games as possible. With Linux I tried the 'memory reduction' technics I learned from Windows 98 (remove unused services, strip down X, only run certain programs, pay carefull attention to memory usage) but then when I would do anything the memory usage would actually go up! I was a bit shocked.

But then I learned about operating systems that actually have effective memory management and Linux's 'Unused memory is wasted memory' concept and it all made sense.

Nowadays to me the most efficient memory management is about how much RAM you can use and still never have to access harddrive swap during heavy use.


Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Your computer needn't be the first thing your see in the morning and the last thing you see at night. (Simon Mainwaring)

Linux posts

Troubleshooting posts

This post tagged:






Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode