Re: [NTLK] [OT] Re: speed: OS X's fault

From: Bob Carls Dudney <kosmicdollop_at_saber.net>
Date: Fri Sep 25 2009 - 14:30:48 EDT

On 22/9/09, Steven Scotten wrote:
|
>What is the difference? ..between swap and virtual memory
Here's my novice picture:

(thoughts on your performance question follow below)

In a nutshell, virtual memory (VM) is something like a database the
OS presents to apps/processes so they don't have to be involved in
keeping track of the actual locations of their computational memory.
The VM system allows each app to assume its assigned memory registers
begin at block 0 and continue without skipping any segments, all the
way up to however much memory the app requests on launch. -- Often
more than total installed RAM is requested by and allotted to each
app! (more below)

Without a VM system, each app would have to include and constantly
run code to keep track of actual RAM addresses, and be involved
whenever RAM gets full and less crucial memory is buffered onto disk
= swap memory. It would be a computational gridlock nightmare: each
app would have to continually query the OS for where RAM is free,
then keep track of hugely variable memory addresses, plus the OS
would have to coordinate with each affected app whenever RAM is
swapped out to disk, or swapped memory is needed back in RAM.

So virtual memory is just that: each app thinks it has contiguous
working RAM memory beginning at block 0, "while in fact it may be
physically fragmented and may even overflow on to disk storage."
quoth Wikipedia: http://tinyurl.com/5gsbf (The VM system also saves
app developers endless headaches fighting other apps over RAM: "My
memory's more important than your memory!")

Swap memory, then, is working memory the VM system removes from RAM
(to free up room for other processes) and temporarily places on the
swap partition (the OS X partition is the default location). At least
in UNIX (incl. OS X), swapping computational memory between RAM and
disk is called paging: a page is the minimum amount of memory swapped
at a time, just over 4K on OS X.

Now I finally understand why VM stats in Activity Monitor are so
outrageously huge, and add up to many times more than RAM. VM allows
each app to think it has gigs of RAM so it doesn't sweat running out.
Most apps only use a small fraction (mostly) of the max RAM they
request, so OS can get away with telling apps they have access to a
total of many times more RAM than installed (even if it's also many
times more than available on disk for swap memory, as with example of
my PB -- details in PS).

The problem with OS X is (for reasons esoteric to me) it starts
swapping/paging way before RAM is full, and keeps swapping to beat
the band, consuming loads of CPU cycles, and slowing apps down hugely
by constantly making them wait while it swaps working memory back and
forth between RAM and disk -- even if RAM isn't maxed out.

-- I'm sure all you tech guys are rolling your eyes at this simple
picture and I much look forward to corrections/clarifications. --

On my PB G4 with swapping on, I was constantly hearing disk activity,
and got lots spinning beachballs = OS X's stop signal while it swaps
between RAM and disk -- especially when switching to an open app I
hadn't used in a while. Opening new web pages was also often enough
to trigger the stop sign, especially if they included multimedia.

(I'm sure wait times are less on newer/faster chips and HDDs.)

So now I finally understand why doubling RAM on my PB produced a very
disappointing, proportionally far smaller performance improvement --
especially compared to Classic OS when doubling RAM would hugely,
often more than double performance (at least it seemed that much).
While more RAM did permit more windows/tabs and apps open,
speed/responsiveness wasn't much faster, if at all.

(It also seems OS X goes crazy with swapfiles, generating far more
than necessary.)

>would love to learn about reducing performance bottlenecks
>with a RAID dedicated solely to Photoshop swap

(I can't imagine why it's useful to protect swap via RAID. Weird.)

I've no experience with an app that does its own swapping, but
presuming Photoshop's swapping is completely independent of VM, if
you have adequate RAM to run Photoshop and your other apps without OS
X needing to swap RAM to disk, you'd probably be very pleased at the
performance boost with swap off (at least on 10.5 or less -- sounds
like Snow Leopard is better at VM. I can't try since PB can't run
10.6.)

The modified dynamic_pager I've not tried sounds reliable if you're
up for compiling that hack: http://tinyurl.com/yrsb6m. As promised,
I'll soon publish much simpler hack I stumbled onto (tested on 10.5
only).

(In a nutshell: one just modifies the file
/System/Library/LaunchDaemons/com.apple.dynamic_pager.plist in some
way that changes its permissions. System then ignores it on boot and
doesn't load dynamic_pager, the binary that does swapping.)

When the system runs out of RAM it freezes, of course = any unsaved
changes are gone. I've set all apps I can to auto-save as frequently
as possible. (Since I've had years of freezes due to running out of
disk space for swapfiles, I was already in habit of manually saving
often myself. -- iCab cooperates beautifully with preference to
automatically remember all open windows and tabs (it just stores the
URLs).)

Above link reports people needing much less than 2 GB RAM, even
running a number of apps simultaneously. But I often get close to
maxing my 2 Gs -- I keep finding new fascinating web pages and
frequently end up with dozens open at a time. Occasionally I need to
close windows and/or tabs or some apps to prevent a freeze. (MemTools
helps hugely keeping enough RAM free. Well worth registering if one
has limited RAM.)

One poster on the linked page says he's found a way to have swapping
turn back on before the system freezes, but AFAIK he's not shared
that on web yet.

Although I've not had a kernel panic on around 20 freezes since
killing swapping,* the freezes stop any and all useful input,
including Dock and its Cmnd-tab app switcher; even force-quit
keyboard shortcut stops working. (Trackpad sometimes still alive, but
can't click nor select.)

  * much less lately since I turned up minimum level of free memory to
trigger MemTools

Since I'm still able to do a forced shutdown by holding power button
down, the kernel doesn't completely freeze (presuming safe powerdown
is a kernel function). Also, there's often still lots of disk
activity after system freezes up. -- Goodness knows what's going on.
So far nothing corrupt I know of. I've tried leaving the machine
alone for even more than an hour to see if it'll recover, but never
has.

The logs show the fs journal replays on startup after a freeze. I
always run fsck on restart* (after any abnormal shutdown): so far
it's always said "..disk..OK." Then it warns ***FILE SYSTEM WAS
MODIFIED***, I presume because journal replayed and completed disk
changes. I then run fsck again (per advice I've read) and then get
just "..disk..OK" like normal.

                       *per single-user mode

-- Ain't fs journalling grand! -- Apple hasn't been entirely standing still.

[ Since I've raised that utterly irrelevant issue, I'll conclude with
the few things I appreciate about OS X vs. OS 9, in this order:

1. protected memory (hooray!) (Although nowadays it's mostly the OS
that craps out, even before I turned swapping off.)
2. Time Machine (but would Classic OS not support the same functions?)
3. WebKit (iCab also uses) -- but not enuf to justify switch from OS 9
4. iWorks is better than AppleWorks, but Quartz lacks some Classic OS
font capabilities, and I don't know any reason one couldn't have same
functionality without Unix. But I don't know programming at all.
5. It's still supported (or was until Snow Cat made my PB obsolete)

There may be some other unique features Unix made possible I've
overlooked but appreciate; it's reportedly more secure, but it sure
adds tons of headaches, and, as mentioned, is memory guzzler
horribilis (both RAM and disk). I presume Column view could be added
to Classic Finder.

-- I'm obviously a guy with little to no interest in Bells and
Whistles, and don't need/want a laptop for gaming nor entertainment =
I just want IT, not ET distractions. :-)

]

Good luck!

B

Example of VM stats: On my PB, Activity Monitor currently reports 40
Gs VM in use, even though only 1.7 Gs of RAM are occupied and there's
0 Kb in my sole, empty swapfile (plus there are less than 3 Gs free
on my OS partition).

I.e, the OS is telling apps they're assigned > 20 times more RAM than
they're actually using (or is actually available: total 2 G RAM
installed; > 8 times if one includes free space on partition
available for swap).

====================================================================
The NewtonTalk Mailing List - http://www.newtontalk.net/
The Official Newton FAQ - http://www.splorp.com/newton/faq/
The Newton Glossary - http://www.splorp.com/newton/glossary/
WikiWikiNewt - http://tools.unna.org/wikiwikinewt/
====================================================================
Received on Fri Sep 25 14:31:02 2009

This archive was generated by hypermail 2.1.8 : Sat Sep 26 2009 - 02:30:00 EDT