Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Move swap to another partition, revisited again System 10.4
Tiger only hintThis hint is based on several prior hints and comments published earlier (see references at the end), and is proven to work under Tiger. Before proceeding, please make sure you are familiar with the following terms: virtual memory, swapfile, root, sudo, terminal. You'll also need a separate partition on which to store the swap files.

This hint makes virtual memory start a bit later so that your swap volume is already mounted, thus eliminating the problems stated in referenced hints. If the swap volume fails to mount, then the boot volume will be used.

Let's start by making a backup of /etc/rc. If something goes wrong, you'll be able to boot from CD or single-user mode, start Terminal, and restore the original file):
sudo cp /etc/rc /etc/rc.saved
Now create a new text file with the following contents (replace the word Swap after the equal sign in the first line with the name of your volume:
swapvolume=Swap

RMRF_ITEMS="${swapdir}/swap*"
if [ ! -d /Volumes/${swapvolume}/.Trashes ]; then
    swapcount=1
    echo "Waiting for ${swapvolume} to mount"
    while [ "$swapcount" -le 10 ]; do
        sleep 1
        if [ -d /Volumes/${swapvolume}/.Trashes ]; then
            echo "${swapvolume} mounted after $swapcount seconds"
            break
        fi
        swapcount=`expr $swapcount + 1`
    done
fi

if [ -d /Volumes/${swapvolume}/.Trashes ]; then
    swapdir=/Volumes/${swapvolume}/.vm
    echo "Using ${swapdir} for swapfile"
else
    echo "Unable to use ${swapvolume} for swapfile"
fi
Save this file to your Desktop as rc.txt and issue the following commands in terminal:
sudo cp ~/Desktop/rc.txt /etc/rc.swap
sudo chown root:wheel /etc/rc.swap
Now we are ready to edit /etc/rc. Type the following line in your terminal to launch TextEdit as root (you may choose another method of course):
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/rc
Find the line (184):
swapdir=/private/var/vm
Insert these three lines of code after that line:
echo Removing $RMRF_ITEMS
rm -rf $RMRF_ITEMS
if [ -f /etc/rc.swap ]; then . /etc/rc.swap; fi # inserted locally
Now select this entire block of code...
if [ -f /etc/rc.swap ]; then . /etc/rc.swap; fi # inserted locally
if [ "${NetBoot}" = "-N" ]; then
    sh /etc/rc.netboot setup_vm ${swapdir}
fi

if [ ! -d ${swapdir} ]; then
    echo "Creating default swap directory"
    mkdir -p -m 755 ${swapdir}
    chown root:wheel ${swapdir}
else
    RMRF_ITEMS="${RMRF_ITEMS} ${swapdir}/swap*"
fi
            
echo Removing $RMRF_ITEMS
rm -rf $RMRF_ITEMS

if [ ${ENCRYPTSWAP:=-NO-} = "-YES-" ]; then
    encryptswap="-E"
else
    encryptswap=""
fi
/sbin/dynamic_pager ${encryptswap} -F ${swapdir}/swapfile
... and move it down to appear just before this line:
/usr/sbin/update
Save the file, reboot, and enjoy.

P.S. To see what directory is used for swap files, you may type the following in Terminal:
ps -wax | grep dynamic_pager -m1
References: [robg adds: There's some debate over the benefits of moving swap, but for those of you who wish to do so, this may be the best solution yet posted here (though I haven't tried it myself). I have marked this hint "Tiger only" for now -- if someone running 10.3 can test it, please let me know if it works for you...]
    •    
  • Currently 2.33 / 5
  You rated: 4 / 5 (6 votes cast)
 
[31,081 views]  

Move swap to another partition, revisited again | 30 comments | Create New Account
Click here to return to the 'Move swap to another partition, revisited again' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Move swap to another partition, revisited again
Authored by: jcbeckman on Jun 17, '05 10:29:48AM

You'll only get a real benefit if you move it to a different physical drive. Partitioning a single drive and then putting the swap on another partition will probably not bring any noticeable improvement.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: DanFrakes on Aug 11, '05 11:00:28PM
In fact, it may result in decreased performance by increasing the time it takes the drive heads to move between the "swap" data and the "non-swap" data.

[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: genericuser on Jun 17, '05 11:41:04AM

What if I do have a second drive available but it's only a 5400 rpm drive. Am I bettter off with leaving the sawp file on the original drive (7200 rpm) ?



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: ChrisAllison on Jun 18, '05 03:26:35PM

Short Version: Keep it on the 7200 HDD.


Long version: While I'm not entirely sure what the protocol for drive-access is during heavy traffic (ie: if access to swap on a main drive would be delayed because of a high load of other process all attempting to access the drive at once), I'd be inclined to think that for most users, such access-conflicts would not be likely to occur.

That having been said, I'd advise keeping your swap partition on the fastest drive available to you. Swap is only employed (as I understand it, at least) when ram is unavailable. HDDs already have a much greater seek time than ram, slowing that further by moving your swap partition to a slower drive would only be shooting yourself in the foot.



[ Reply to This | # ]
Move swap: another benefit, for small disks
Authored by: FlaSheridn on Jun 17, '05 12:13:45PM

Another benefit to moving your swap partition, at least on disks that are low on space: If a memory hog fills up your main partition with swap space, many applications will destroy their preference files when they quit. Moving the swap file avoids this problem, at least pre-Tiger. (I haven't upgraded my old TiBook, so I don't know whether this is still a problem with Tiger.)



[ Reply to This | # ]
Move swap: another benefit, for small disks
Authored by: Tom Robinson on Jun 21, '05 04:00:08AM

I can confirm that with 10.4.1, at least SecuritySpy eats its preferences if the disk fills up.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: MacManiac on Jun 17, '05 01:01:13PM

I've been using a dedicated swap partition on my G3 Powerbooks (both Lombards and Pismos) since 10.1.3 --- when I installed Tiger on a 60GB expansion bay drive to explore its' features/flaws, I chose to leave it unmodified.

FWIW, while having swap on a separate partition may not enhance performance directly, I have found that having swapfiles separate from the rest of my data has been effective in minimizing drive fragmentation and directory issues.

I also maintain a small bootable partition for OS 9.2.2 for those rare times when I might need it......I still run MacDraw Pro for a few projects.

After more than a month of flawless performance, this looks like it may be my first "under the hood" maneuver with Tiger.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: mattyturner on Jul 12, '05 04:57:09AM
FWIW, while having swap on a separate partition may not enhance performance directly, I have found that having swapfiles separate from the rest of my data has been effective in minimizing drive fragmentation and directory issues

I thought swapfiles were always contigious anyway?



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: MacManiac on Jul 15, '05 10:43:00AM

"I thought swapfiles were always contigious anyway?"

While each swap file may be contiguous, when more than one is generated there is no guarantee that each will be contiguous to the next......and on every shutdown/restart a fresh swapfile is created to replace the one(s) that were removed on restart.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: AGDenton on Jun 17, '05 01:02:47PM
Unfortunately, this hint seems to fail when you point to a partition on another disk : the desired partition never mounts early enough, causing the script to put back the swap to /private/var/vm. Under 10.3.x, assigning a UUID to the partition, putting it in fstab and inserting a
/sbin/autodiskmount
sleep 1
in
/etc/rc
while enabling disk mounting without user login using the hint on this site had been working for me. However, partitions on other disks never seem to mount before login in Tiger, no matter what I do. Any ideas?

[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: club60.org on Jun 17, '05 05:49:21PM
I've setup several machines this way. All of them using separate disks for swap. Maybe you are using non-IDE disk? Then you can try to issue this command in terminal:
sudo defaults write /Library/Preferences/SystemConfiguration/autodiskmount AutomountDisksWithoutUserLogin -bool true


[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: diffident on Aug 07, '05 12:01:07PM

I don't really like the method used, because delaying the vm startup until everything else has started (ie everything in /etc/mach_init_d, System/Library/LaunchDaemons, Library/LaunchDaemons and so on) is a pretty fundamental change to the system startup, but I can confirm that this hint does put the swap files on the volume specified in rc.swap, even if that volume is on a separate IDE disk (Tiger 10.4.2).

For my own machine, I've added my swap disk to etc/fstab (had to create fstab) so it's explicitly mounted by the limited mount call early in Tiger's rc. That means Dave Bayer's original and far less intrusive method (on which this hint is based) works just fine under Tiger as well as Panther, albeit with the disk-name caveats Dave mentions on his very informative page (I've made a few further modifications to avoid some of those issues, but they're very specific to my setup).

Given that Apple are deprecating the use of rc in favour of launchd, neither this hint nor Dave's original are going to be long-term solutions for moving swap in Tiger, so with every OS revision anyone using these hints will need to check whether Apple have made changes to rc.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: aschmidtm on Jun 17, '05 10:52:15PM

Will this work if I use .swap?

---
[-[-[A]-]-]



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: MacManiac on Jun 18, '05 02:40:03PM
Re: ...will this work with .swap?

I haven't gone back to that naming convention since it was "broken" for us in Panther.....I had been using ".swap" as the name for my swap partition in previous releases of OS X, but somewhere along the line in Panther that became an illegal option --- It may be acceptable in Tiger again, but when I implemented this hint, I didn't try that.

I use a coupla fine-tuning modifications to this method of moving the swap directory. First, I've found that making an fstab to mount the partition early has worked well. There has been some discussion that the OS can change the actual /dev/disk assignment on the fly, but I haven't seen that on my installations.....so for my fstab, I simply query the Terminal with df to find the device listing for my swap directory, then use that info to make my fstab as follows:

more /etc/fstab

/dev/disk0s10 /Volumes/swap ufs rw 1 2
Second, as you can see above, I use the Unix File System for my swap partition....this makes the partition invisible in OS 9 and seems to take care of a whole host of secondary issues such as fragmentation.

Third, I add one additional line to the rc.swap file shown above so that it will use fsck to "preen" the swap directory in the event of an unusual shutdown.....this works really well.

The additional line goes right after the swapcount=1 line as shown below:

swapvolume=swap

RMRF_ITEMS="${swapdir}/swap*"
if [ ! -d /Volumes/${swapvolume}/.Trashes ]; then
    swapcount=1
    fsck -p /dev/disk0s10
    echo "Waiting for ${swapvolume} to mount"
    while [ "$swapcount" -le 10 ]; do
        sleep 1
        if [ -d /Volumes/${swapvolume}/.Trashes ]; then
            echo "${swapvolume} mounted after $swapcount seconds"
            break
        fi
        swapcount=`expr $swapcount + 1`
    done
fi

if [ -d /Volumes/${swapvolume}/.Trashes ]; then
    swapdir=/Volumes/${swapvolume}/.vm
    echo "Using ${swapdir} for swapfile"
else
    echo "Unable to use ${swapvolume} for swapfile"
fi
So I now have a separate swap directory formatted in ufs on a different drive that mounts early and self repairs if shutdown abnormally.

ymmv, but this technique has been working for me on a single drive with a dedicated swap partition in ufs since the introduction of Panther.....and now in Tiger!

[ Reply to This | # ]

Move swap to another partition, revisited again
Authored by: forty-2 on Jun 18, '05 06:29:28PM

How about multiple swap volumes across multiple disks? I saw a marked improvement when I did this under Linux, as linux stripes across multiple swap volumes. I'd also recomend placing your swap partition somewhere in the middle of the drive for better seek times.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: Shawn Parr on Jun 20, '05 04:37:48PM

Why the middle of the drive?

The first section of the drive is technically faster than the middle or end of the drive, and most linux installers will put the swap towards the beginning by default, unless of course you override the defaults.

If you are looking to increase performance you should have swap at the beginning of the drive to give it a speed boost.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: forty-2 on Jun 26, '05 08:45:18PM

Because eventually you're going to use most of drive, which means the head could be anywhere on the platter. While the R/W speed is greatest at the begining of the drive, the seek time is faster somewhere in the middle.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: bdog on Jun 18, '05 08:58:06PM

I've found the easiest way is here:

http://www.bombich.com/mactips/swap.html

It worked for me the first time, not the second time, then again the third time. The first time was some test machine. The second time was my iBook, then since it didn't work I re-partitioned, re-installed the OS, then did the hack again and it worked... Very strange. It is definetly a hack you want to do just after you've installed the OS, or at least right after you've backed up your system.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: g3ski on Jun 20, '05 04:24:59AM

For those who want to move swap without all of the terminal fun, Xupport makes it easy. I have it on a variety of 10.3.x setups with various drives and paritions.

I have also noticed (there is a thread about this somewhere) that while activity monitor reports massive amounts of VM usage, 11GB right now for me, the swap files max out at about 1.5-2 times total ram. I have 2GB and after running a bunch of apps to eat up ram (iphoto, photoshop major image resizing, lots of QT movies, etc) I have been unable to create more than 3GB worth of swap files.

I can't say that I notice the difference in speed on a DP 867 MDD, but I think it would be useful on older SP Macs like 'books.

---
"I want my two dollars!"



[ Reply to This | # ]
Tweak dynamic_pager to make better use of small swap partitions
Authored by: adrinux on Jun 21, '05 03:37:05PM

I can confirm this hint works on my G4 with the swap partition on the original 60Gb drive and my 160Gb as the boot partition. (That said be careful about cutting and pasting the rc.txt contents from the tip, using BBEdit I ended up with a bad character that killed /bin/sh and brought startup to a halt - Terminal.app on the Tiger install DVD comes in handy :).

But one other thing I was doing in Panther was changing the dynamic_pager settings to more closely match those in Jaguar. If you've moved swap to a different partition that's relatively small, or even if you've not moved swap at all and just have a small amount of space on your main drive changing the pager settings may help dynamic_pager to make better use of the available space.

By default dynamic_pager in Panther and Tiger creates swapfiles in exponentially larger sizes. The first is 64Mb, the second 64Mb, the third 128Mb, then 256Mb, 512Mb, 1Gb, 2Gb etc. So if your swap partition is 3Gb and you've already got to the stage of having 2Gb of swapfiles dynamic_pager will try to create the 2Gb swapfile and fail because there is only 1 Gb left - clearly ridiculous, especially if only a few more Mb of swap space are needed.

What you can do is tell dynamic_pager to use fixed size swap files. Here's what I did after applying the hint above. Find the line (265 in my rc after moving swap) in /etc/rc that says:

/sbin/dynamic_pager ${encryptswap} -F ${swapdir}/swapfile

and comment it out then add a new line:

/sbin/dynamic_pager ${encryptswap} -H 40000000 -L 320000000 -S 256000000 -F ${swapdir}/swapfile

This will cause dynamic_pager to create swapfiles of 256Mb in size, larger than the 80Mb used in Jaguar, but not too big. You can of course use different numbers but please read

man dynamic_pager
first! The relative size of these three numbers is very important!



[ Reply to This | # ]
Tweak dynamic_pager to make better use of small swap partitions
Authored by: adrinux on Jun 21, '05 03:58:33PM
Ok, so this creates 244Mb swapfiles :)

It's one of those bits vs bytes things isn't it?



[ Reply to This | # ]
Make swap partition invisible?
Authored by: dm2243 on Jul 14, '05 03:02:34PM

Has anyone found that making the swap partition invisible creates any problems, when following this hint?

Oh, and how do you make the swap partition invisible?

thanks



[ Reply to This | # ]
Make swap partition invisible?
Authored by: bdog on Jul 17, '05 02:23:29AM

Begin the name of the volume with a period . You can also use a utility to set the invisible flag.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: yangzone on Aug 03, '05 08:21:01PM

Tiger 10.4.2 Everything seemed to go well but the swapfile is not being moved to my swap volume for me. When I check for it's location I get:
59 ?? Ss 0:00.00 /sbin/dynamic_pager -F /private/var/vm/swapfile

'Supp?

PJ



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: johnpitcairn on Aug 18, '05 12:28:04AM

I don't think this method is reliable - Tiger seems to want vm started early, moving the code block is not recommended. It works, but there are consequences.

The primary symptom of trouble on my system (fresh install, 10.4.2) is that if vm is started later in the boot process, .dmg disk images will refuse to mount - DiskImageMounter returns a "device not installed" error.

BTW, I definitely don't recommend the method on Mike Bombich's site - that hasn't been updated since 10.1.x.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: johnpitcairn on Aug 18, '05 01:44:07AM

OK, I think I have it working with no ill effects. I'm using a swap partition on the same disk as the boot partition, simply because I'm usually short of space on my TiBook and want to prevent data getting stomped on if the boot partition fills up.

The parent post (which seems to be almost entirely Mike Bayer's method, which works unmodified in Panther) is good, but don't move the virtual memory code block later in the rc file.

Then, add an entry to your /etc/fstab file (which you may need to create) so the swap partition is mounted when / mounts. My entire fstab file looks like this:

/dev/disk0s12 /Volumes/Scratch hfs rw 1 2

You can get the appropriate partition data via the command df -k in the Terminal.

Finally - and this may not be necessary but I don't think it'll hurt - load the fstab data into NetInfo via:

sudo niload -m fstab / < /etc/fstab

You're done. Reboot in verbose mode by holding down Command-V at boot, so you can see what the mount process and VM starter is doing.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: johnpitcairn on Aug 18, '05 01:45:18AM

OK, I think I have it working with no ill effects. I'm using a swap partition on the same disk as the boot partition, simply because I'm usually short of space on my TiBook and want to prevent data getting stomped on if the boot partition fills up.

The parent post (which seems to be almost entirely Mike Bayer's method, which works unmodified in Panther) is good, but don't move the virtual memory code block later in the rc file.

Then, add an entry to your /etc/fstab file (which you may need to create) so the swap partition is mounted when / mounts. My entire fstab file looks like this:

/dev/disk0s12 /Volumes/Scratch hfs rw 1 2

You can get the appropriate partition data via the command df -k in the Terminal.

Finally - and this may not be necessary but I don't think it'll hurt - load the fstab data into NetInfo via:

sudo niload -m fstab / < /etc/fstab

You're done. Reboot in verbose mode by holding down Command-V at boot, so you can see what the mount process and VM starter is doing.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: bdog on Aug 20, '05 11:20:00PM

THE METHOD ON BOMBICH'S SITE, ALTHOUGH OLD, WORKS FINE EVEN ON TIGER! IT'S SO SIMPLE IT HASN'T BROKE.



[ Reply to This | # ]
Move swap to another partition, revisited again
Authored by: andrewz on Aug 26, '05 09:19:42AM
I've got a PowerBook with Mac OS X 10.4.2, with partitions for the operating system (10 GB), media (30 GB), and a small spare partition for Panther/Classic (3 GB). The boot partition got so full and fragmented that virtual memory swapping just brought the computer to its knees. Switching users took like 30 seconds.

I decided that the 3 GB partition would serve better as a swap parition, since I never really need to boot into Panther or Classic anymore. (I burned a DVD of the partition first, so it'll be easy to get back if I want to.)

I tried Bombich's method because it looked the simplest and comments above suggested that it worked fine on Tiger. I can confirm that it works great. Just to be safe, I tried the first time with the "mount -a" command and it worked right off the bat.

The difference is like night and day. I was getting ready to buy a new computer because mine had become so slow, but this simple change has restored the speed that the computer had when it was new!

[ Reply to This | # ]

Move swap to another partition, revisited again
Authored by: conrad on Jun 19, '07 10:08:31PM
This hint will not work on Intel-based Macs since they use GUID partition tables. An updated version of Dave Bayer's script that works on Intel-based Macs is available at gizmometer.

[ Reply to This | # ]