
Tired of explaining Linux's seemingly obtuse memory usage to confused newbies? - yankcrime
http://www.linuxatemyram.com/
======
colanderman
This is misleading, as the kernel will often swap out processes rather than
shrink the disk cache. Its tendency to do this is controlled by the
vm.swappiness parameter, which by default heavily favors swapping:
<http://kerneltrap.org/node/3000>

Decreasing this value will favor reducing the cache size and will tend to
increase responsiveness. You can set it in the /etc/sysctl.conf file. Low
values (≤ 5) are especially good for SSDs where you want to minimize swap
usage.

~~~
jerf
I often just turn swap off entirely. A normal workload on my 4GB linux machine
will frequently fail to fill even 2GB of _buffers_. At the moment my machine
has still managed to fail to fill 800MB of my RAM with anything and I'm
running two virtual machines and doing my usual _work_ load.

I do have a swap partition but only so I can hibernate.

~~~
requinot59
_> I often just turn swap off entirely._

And you're smart to do so. Swap is useless for 99% of end-user systems.
ChromeOS doesn't use any swap partition, for instance.

As for hibernation, it's not available if you use a crypted swap, which is
wise to use (at least on a laptop).

As a result, I, too, disabled swap completely.

~~~
jerf
"And you're smart to do so."

Finally, that's the first comment I get, instead of someone extolling the
virtues of swap and babbling warmed-over 1990s rules of thumb about "twice the
RAM is the recommended size of your swap file" as if I'm going to wait for
even 200MB of swap to fill up before flipping out and killing the offending
process, let alone 8GB.

~~~
cookiecaper
Hibernation requires a large swap space, doesn't it? Last I knew it did,
perhaps you can compress it now.

~~~
jerf
Yes, and it may seem a merely semantic difference, but there's still a
difference between "swap space" and "hibernation backing". One I'm willing to
wait for while it fills, the other, I am not. The kernel may not distinguish,
but I do. When I don't care about hibernation I just remove it.

~~~
cookiecaper
Right, swapoff is totally acceptable imo if you're having trouble with swap. I
was commenting on how "double the size of RAM" is a bad or outdated guideline.
There's been a few times where I've regretted not making my swap space big
enough, sometimes when getting a RAM upgrade and wanting to do hibernation,
etc. My disk isn't super pressed for space so in my mind there's no real
reason to be stingy, and then you don't have to grow/shrink partitions if an
upgrade occurs after the initial disk setup.

------
spudlyo
_You can't disable disk caching. The only reason anyone ever wants to disable
disk caching is because they think it takes memory away from their
applications, which it doesn't! Disk cache makes applications load faster and
run smoother, but it NEVER EVER takes memory away from them! Therefore,
there's absolutely no reason to disable it!_

Never say never. You can and should disable/purge linux buffer caching in
certain situations. See O_DIRECT and/or POSIX_FADV_DONTNEED. If I'm writing a
database, I take care of my own caching, I do not need the OS doing it as
well. Also, if I'm doing a huge rsync of a directory tree, just because I just
read every byte of every file in that tree, I most certainly _do not_ want to
load all of that crap into the linux buffer cache. It will evict my already
warm set, as well as potentially cause a ton of swapping.

~~~
js2
Funny, I just noticed this in the Mac OS X rsync man page for the very first
time:

    
    
           --cache
                  Apple specific option to enable filesystem caching of rsync file
                  i/o Otherwise fcntl(F_NOCACHE) is used to limit memory growth.

~~~
Nick_C
There was a patch for all architectures, but it got dropped after 3.07
(current is 3.09). Andrew wrote "I don't feel this patch is general enough nor
straight-forward enough to make it into rsync."

See these for more: <http://insights.oetiker.ch/linux/fadvise/>
<https://bugzilla.samba.org/show_bug.cgi?id=7004>

------
StavrosK
A good way I read somewhere was "Unused RAM is wasted RAM. Linux uses it to
speed up your disk and gives it back to programs when they need it".

------
eliben
The title of the link (unlike the linked page) suggests that Linux is somehow
to blame here... Why? Using RAM for disk caching is _a good thing_.

Besides, this technique is employed by Windows as well. It's just that the
average Windows user probably doesn't know how much free RAM his PC has (well,
the really _average_ user doesn't know how much total RAM it has). My Windows
7 box takes at least 2GB of memory to itself for disk caching, AFAIU.

~~~
roc
Windows started doing disk caching with Vista and users _certainly_ noticed
and complained in much the same way.

~~~
barrkel
Windows didn't start doing disk caching with Vista. Perhaps it used memory
more aggressively, or changed the way it did the accounting, but it certainly
didn't _start_ doing disk caching just with Vista, as anyone who remembers the
difference in performance of DOS before and after running SMARTDRV will
attest.

~~~
ringm
2000/XP was always extremely aggressive about allocating memory for cache. It
would very often swap stuff out to increase available cache, which made the
apps you did not use for just a few seconds completely unresponsive.

~~~
barrkel
Specifically, minimized applications got their working set trimmed. You could
see this in Task Manager: it's Memory Usage column was actually Working Set (I
think they're more explicitly labelled these days) and it frequently went from
many MB down to a few KB once you minimized the app. Working set is the set of
pages, currently in memory, that the app has used "recently", and are thus
less likely to be paged to disk. Shrinking the WS makes pages more likely to
be paged out.

------
wzdd
This paragraph: 'But what do you call memory that is both used for something
and available for applications? You would call that "free", but Linux calls it
"used".' is IMO the best way I've ever seen of explaining the issue. It also
reveals the solution: call it free! It is free, after all. Nobody cares about
how much free ram is cache unless they are doing something really specific, in
which case they should be passing specific flags, or using a specific tool, to
report it.

Unfortunately there is no chance Linux will do this, as the relevant reporting
tools are too entrenched (and with good reason).

~~~
wrs
Or just rename "free" to "wasted". I usually tell people the free number
indicates how much RAM they paid for but aren't using.

~~~
sliverstorm
It is admittedly difficult to re-learn what you know about computers, and what
Windows has taught people (including myself) is that RAM usage must be below
~75% for optimal performance. It has been my experience that paging becomes
much more blocking above that.

------
stcredzero
Hmm, this situation is like owning an electric vehicle. The manufacturers are
passionate about technology, and they invented a flywheel that can store
massive amounts of energy, giving your vehicle incredible transient
acceleration response. They ship many vehicles with this new feature, but
unfortunately, they forgot to account for what this would do to the battery
charge display after starting. As a result, their reps keep on having to
explain that the vehicles are not rapidly leaking charge shortly after
starting -- instead that energy is being placed in the flywheel where it can
greatly increase the car's responsiveness.

Whoops!

The difference between the scenario above and Linux's disk cache? Techies
blame the former on the car company but the latter on the end-users.

~~~
Andys
If only electric cars were free like Linux.

~~~
stcredzero
My analogy has more to do with approaches to UX than it does with economic
model.

------
slavak
Wouldn't it make more sense to just include the RAM used for disk-caching when
calculating free memory? After all, the whole reason it's being used for disk-
caching in the first place is that it's free! The Linuxoids that know what
this even means can always explicitly look for the line that says how much
memory is being used for caching...

~~~
barrkel
It costs time to zero pages; disk cache pages are not zeroed. Not having a
source of zero pages will add latency to large memory allocation requests.

(The pages must be zeroed to avoid the security problem of snooping on data
you might not have access to.)

~~~
slavak
Exactly the kind of thing users not familiar with the way Linux handles memory
DON'T consider when they check how much free memory they have. If you're the
sort of person concerned with the overhead of zeroing pages then you can look
at more detailed memory-usage info to find the details you're looking for.

------
ktf
It looks like gnome-system-monitor reports the "correct" amount of free RAM.
I'd imagine most newbies would use that rather than popping open a terminal
and typing "free," right?

~~~
NonMint
You'd be surprised. Some users who use Linux consider themselves to be pretty
adept, and I can easily see this problem being an issue for people who
perceive themselves to be clever.

------
pixelbeat
Heh, nice site.

Note I find this useful for showing how much RAM apps really are using:
<http://www.pixelbeat.org/scripts/ps_mem.py>

------
ludwigvan

        free -m | awk 'NR==3 {print $4}'

------
radu_floricica
I have had the same problem, plus some, when trying to guesstimate memory
usage in vps servers. The caching is transparent, so free always reports zero
swap. ps gives sensible numbers, but the total used memory is neither RSS nor
VSZ summed. I've heard about library memory being double-counted, but I've
found no way to accurately get the numbers. In the end I pretty much gave up
and settled for the settings that worked.

tl;dr: on vps things can be even uglier.

------
jawee
All this does is make me regret paying more for extra memory... With a dozen
programs open, I am sitting at 2.3GB free, according to this. I guess it was
wrong to splurge for that extra memory.

------
cullend
So basically the same thing Vista was doing (prefetch, and admittedly it did a
shitty job with >1GB ram), except since its Linux, Oh its all fine guys, we
won't destroy the OS' reputation.

~~~
samdk
Linux has been doing this for a lot longer than Windows has. (Which I only
actually know because there was so much FUD about Vista's pre-fetching when it
came out.)

 _edit:_ Sorry, getting prefetching and SuperFetch confused.

~~~
barrkel
MS shipped SMARTDRV with DOS 4.01, which shipped in 1989. By my reckoning,
Windows (running on DOS) has had disk caching since before Linux existed.

------
JonnieCache
How does this compare to darwin's memory management?

------
Florin_Andrei
It's merely a matter of definition - whether to include the disk cache in the
"used" memory or not.

------
jefe78
I love explaining it! The response is priceless, "Wow, that's pretty cool!"
"No, that's Linux."

