Gentoo Archives: gentoo-user

From: Grant <emailgrant@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] mysqld invoked oom-killer
Date: Thu, 21 Jul 2011 23:54:37
Message-Id: CAN0CFw0UcULzorcdycXdiKVG94YVt+xv7RJfzGrLp8UaB-PTQA@mail.gmail.com
In Reply to: Re: [gentoo-user] mysqld invoked oom-killer by Albert Hopkins
1 >> It sounds like adding physical RAM is better than enabling swap in
2 >> every way.  I'll stay in the anti-swap camp.
3 >
4 > I don't see why it has to be one way *or* the other...
5 >
6 > Yes more RAM is always going to be better than more swap, RAM is just
7 > way faster than disk, however byte-per-byte, disk is cheaper.
8 >
9 > The whole reason why we have swap.. back in the olden days, some
10 > programs needed more RAM than perhaps the system provided.  Some of
11 > these program were written with this in mind, and actually handled this
12 > by manually writing some of it's data to disk, then freeing that data
13 > from RAM, doing something, then when it needed the disk data, reading it
14 > back into RAM (after having freed the previous data).  This is of course
15 > cumbersome.  Enter virtual memory operating systems, which basically
16 > treat fast memory (RAM) and slow memory (disk) as one flat memory pool.
17 > Then the program it all looks like memory, and the OS does the paging in
18 > and out to disk.
19 >
20 > By now you would think "oh, but if I just had one system that had a more
21 > RAM than i would ever use simultaneously, then I don't need swap, right?
22 > Well, not exactly, because modern operationg systems also do something
23 > called filesystem caching.  What this does is, recently, and often used
24 > (parts of) files on the filesystem are kept into fast RAM, so when a
25 > program needs that data it can be fetched from cached RAM instead of
26 > hitting slower disk.  Ok, that's nice, but what does that have to do
27 > with swap?  Well, not only does Linux keep track of what files are used often, it
28 > also keeps track of what pages of virtual memory are *not* used often.
29 > Say you started some program a long time ago, or some program launches
30 > at boot time, but that program sleeps and doesn't do anything for a long
31 > time.  Now ordinarily that program would just sit there taking up RAM.
32 > Now you are running some other programs, and these programs are very
33 > actively hitting the disk.  Now Linux would love to use more RAM for
34 > caching those disk hits, but that program you haven't touched in hours
35 > is taking up RAM doing nothing.
36 >
37 > That's where swap comes in.  Linux would like to take that sleeping
38 > process and swap some of its pages out to disk, so it can use the freed
39 > RAM for more cache, and therefore speed up the programs that are
40 > actually being used.  Then, if the barely used process ever does wake
41 > up, Linux can expire some cache and put it back into RAM.
42 >
43 > In this case swap *is* good because it's making more efficient use of
44 > RAM by swapping out seldom-used processes and using that RAM to cache
45 > often-accessed files.
46 >
47 > So a healthy combination of swap and RAM *can* be a good thing.  If,
48 > however, you have so much RAM that you can run every program you'd ever
49 > run simultaneously with every file you'd ever access cached in RAM then
50 > I wouldn't worry about swap ;)
51
52 So swap isn't treated exactly like RAM. It actually has special
53 handling in Linux which makes it beneficial to have on almost any
54 Linux system? According to Alan, things get very bad when a Linux
55 system hits swap. How can behavior like this be beneficial:
56
57 "When a linux machine hits swap, it does so very aggressively, there
58 is nothing nice about it at all. The entire machine slows to a
59 painstaking crawl for easily a minute at a time while the kernel
60 writes pages out to disk, and disk is thousands of times slower than
61 RAM.
62
63 It gets so bad that you can't even run a shell properly to try and see
64 what's going on and kill the actual memory hog."
65
66 Also, aren't you likely to wear out your hard disk sooner using swap?
67
68 - Grant

Replies

Subject Author
Re: [gentoo-user] mysqld invoked oom-killer kashani <kashani-list@××××××××.net>
Re: [gentoo-user] mysqld invoked oom-killer Albert Hopkins <marduk@×××××××××××.org>