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

Move your swap file to another drive System
By default, Mac OS X uses a swap file (for virtual memory) which is installed on the same drive as your operating system. For best performance, the swap file would ideally be located on the fastest drive in your machine, which may or may not be the same disk as your OS. Unfortunately, there's no built-in GUI for changing a swap drive location.

A few days ago, there was a help request on swapping on another hard drive and subsequent comments that figured out how this could be done. I asked if patpro, one of the users involved, would mind writing a step-by-step instruction set on how to transfer swap. Read the rest of this article to see what he had to say ... well worth the time if you have a spare, fast hard drive in your system!

patpro writes: "Well, I'd be glad to help you with that, but in fact, Sven had a lot to do with my success as well:)). He pointed out some important aspects of the boot process (mount especially)."

With that said, here are patpro and Sven's instructions on relocating your swap file.

NOTE: This hint now reflects the additional functionality submitted by John in the comments
  1. Launch a terminal session.

  2. Backup the rc file into the current directory, which should be ~/ for the current user:
    cp /etc/rc .
  3. Print a partition map, which shows every device mounted on your system, by using df:
    df
  4. In the df output, choose your favorite disk/partition and remember its reference. Each row will look something like this:
    /dev/disk0s9       3134688     1970568  1164120    62%    /apps
    There will be one line for each volume in your system.

  5. During the boot process, mounting a disk other than local file system (MacOS X system drive) appears to occur after swap initialization. So now we're going to edit /ect/rc so that our preferred swap disk is mounted before swap initializes. To do this, we need to become root, transfer to the directory, and edit the file:
    su root
    cd /etc/
    pico rc
  6. In the rc file, replace this
    swapdir=/private/var/vm
    
    # Make sure the swapfile exists
    if [ ! -d ${swapdir} ]; then
    ConsoleMessage "Creating default swap directory"
    mount -uw /
    mkdir -p -m 755 ${swapdir}
    chown root:wheel ${swapdir}
    else
    rm -rf ${swapdir}/swap*
    fi
    with this
    swapdir=/disk-name/path_to_swap_directory
    
    # Make sure the swapfile exists,
    # first check existence
    # if not first make sure disk is mounted
    if [ ! -d ${swapdir} ]; then
    ConsoleMessage "mounting directories"
    mount -t hfs /dev/disk**** /disk-name
    mount -uw /
    fi
    # Make sure the swapfile exists,
    # if not, create directory since disk should be mounted
    if [ ! -d ${swapdir} ]; then
    ConsoleMessage "Creating default swap directory"
    mkdir -p -m 755 ${swapdir}
    chown root:wheel ${swapdir}
    else
    rm -rf ${swapdir}/swap*
    fi
    BE CAREFUL: If your preferred disk is NOT hfs you MUST provide the mount command with it's correct file format (mount -t ufs, for example). So the 'mount' line must follow this framework:
    mount -t (disk file format) /dev/(disk-reference) /(disk-name)
  7. Save the file and quit with CTRL-X, Y, and ENTER.

  8. Reboot
That should do it; your swap file should now be on the drive and directory you specified. patpro finishes with: "Thanx to Sven who helped me a lot figuring out what was wrong in my previous attempts."
    •    
  • Currently 1.93 / 5
  You rated: 3 / 5 (15 votes cast)
 
[89,569 views]  

Move your swap file to another drive | 38 comments | Create New Account
Click here to return to the 'Move your swap file to another drive' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
It works
Authored by: John on Dec 22, '00 09:40:03PM

Yes, I tried this. Just be careful that you enter the correct /dev/disk* value. ;-)



[ Reply to This | # ]
bug in rc script
Authored by: John on Dec 23, '00 12:19:25AM
The problem with the solution as presented above by robg is that, when the rc script detects that swapdir does not exist, it both mounts the disk and creates the directory. If the swapdir was on a previously umounted disk, then a new directory (with a number appended) gets created. The new directory is local on / and not on the desired physical drive. I worked around the problem by skipping steps 6. and 7. Instead just replace
swapdir=/private/var/vm # Make sure the swapfile exists if [ ! -d ${swapdir} ]; then ConsoleMessage "Creating default swap directory" mount -uw / mkdir -p -m 755 ${swapdir} chown root:wheel ${swapdir} else rm -rf ${swapdir}/swap* fi
with
swapdir=/disk-name/path_to_swap_directory # Make sure the swapfile exists, # first check existence # if not first make sure disk is mounted if [ ! -d ${swapdir} ]; then ConsoleMessage "mounting directories" mount -t hfs /dev/disk**** /disk-name mount -uw / fi # Make sure the swapfile exists, # if not, create directory since disk should be mouned if [ ! -d ${swapdir} ]; the ConsoleMessage "Creating default swap directory" mkdir -p -m 755 ${swapdir} chown root:wheel ${swapdir} else rm -rf ${swapdir}/swap* fi


[ Reply to This | # ]
Nice find...
Authored by: robg on Dec 23, '00 01:03:03AM

Thanks for finding the bug in the script! As I said at the end, I hadn't tried it myself yet due to
a lack of extra hard drive space (and hard drives in general!). Patpro (who wrote the how-to)
has said it was working fine on his machine, but I can see how your changes will make
it a safer script. Thanks again for the fix!



[ Reply to This | # ]
other swap options?
Authored by: Anonymous on Mar 25, '01 07:10:58PM

I've got 1G of ram. I want to create atleast 1.5G of swap area to have a total of 2.5G Vmem.

MacOS created by default a swap file in it's root filesystem. This is not what I want. I read about the way to move the swap file to another filesystem that can exist on a faster drive. This is also not what I want. My root filesytem is a 18G scsi.

What I want is for MacOSX to be able to do what other unix flavor can. Swap out to a device, or a raw partition rather than a swap file in a filesytem. From what I see, it's not possible, but since macosx is a unix derivative, it has to be some hidden feature. To swap to a raw partition has dramatic performace increase over to a file in a filesystem.

I've even pre-created 1.5G of raw space in start at the first block of my drive, also to help improve swap performace. But it's idle and reserved for now until I can find out how to use it. Can MacOSX do this? Linux can, Solaris can, IRIX can. OpenBSD, NetBSD, FreeBSD can. I will not believe that OSX can't.

Has anyone looked into this?

Also can a multiple swaps be used? From what I understand in the Unix world, when creating multiple swap area across different drives, the os stripes between them, dramatic improving the speed of read/write to it. I want to be able to explore to performace potential of osX. I'm sure you guys do too. Let's look into this!

Thanks!



[ Reply to This | # ]
other swap options?
Authored by: patpro on Apr 27, '01 07:27:58AM

you may want to take a look at : http://www.peak.org/~luomat/articles/FAQs/Swapfile_Swapdisk/

As swap system of OSX is inherited from the Next's one, these readings are very informative. I based my own research on these docs to find out the way to move swapfiles.



[ Reply to This | # ]
other swap options?
Authored by: chabig on Dec 26, '01 11:55:42AM

If you've got 1GB of physical RAM, congratulations! You're doing the best you can to manage your memory.

The OS X virtual memory system only pages memory to disk when it's necessary. It doesn't reserve disk space in advance, rather it grabs 80MB chunks of disk space only when they're needed.

Chances are, with your 1GB of RAM, you never have any memory paged to disk. When you use all of your RAM and the first 4096 KB chunk gets paged to disk, it will go to an 80MB swapfile. If you task the system so hard that that file is all used up, OS X will create another 80MB swapfile, etc.

You shouldn't think "I want 2.5 GB of virtual memory." In fact, you've already got that, and more--up to the capacity of your hard disk. Got 5 gigs free on your hard drive--congratulations, you've got 6 GB of virtual memory! Got 10 gigs free, you've got 11 GB of virtual memory!



[ Reply to This | # ]
other swap options?
Authored by: patpro on Jan 02, '02 04:41:22AM

I don't agree with your statement saying that "Chances are, with your 1GB of RAM, you never have any memory paged to disk".

I have 640 MB RAM (ok, not 1GB) but I still managed to create about 10 swap files (76,3 MB each) just by opening a 15 or 16MB JPEG with preview.app. other app running were : IE, ircle, finder.app, dock.app, terminal.app, that's all.

So I would say, chances are with 1GB of RAM you will have memory paged to disk sooner or later.

note that OS9 can open the same JPEG and even a 32MB JPEG without virtual memory, and it does it really faster than preview.app on OSX



[ Reply to This | # ]
Update for X final
Authored by: bombich on Mar 30, '01 12:36:25AM

I struggled with this one for a while. I even managed to have no swapfile at all a few times (wow, I really, really, really do not recommend this, I'm targeting this at people who think they are frustrated with X's virtual memory and want to turn it off). Anyway, I thought an updated example would be beneficial to anyone trying this technique with X final.

Here is the code I used in /etc/rc:

swapdir=/Volumes/swap/vm

# Make sure the swapfile exists,
# first check existence
# if not first make sure disk is mounted
if [ ! -d ${swapdir} ]; then
ConsoleMessage "mounting directories"
mount -t hfs /dev/disk0s6 /Volumes/swap
mount -uw /
fi

# Make sure the swapfile exists,
# if not, create directory since disk should be mounted
if [ ! -d ${swapdir} ]; the
ConsoleMessage "Creating default swap directory"
mkdir -p -m 755 ${swapdir}
chown root:wheel ${swapdir}
else
rm -rf ${swapdir}/swap*
fi

Changes from prev posts: Accomodate that volumes are now stored in the /Volumes directory, gave an example of the disk in the code (disk0s6 is my disk's address and its name is "swap", you'll need to change those to accomodate your partition/disk) -- those dang stars kept throwing me off, so I gave a real life example. Good luck!



[ Reply to This | # ]
Clarification
Authored by: bombich on Apr 23, '01 10:02:44PM

Check out <a href="http://www.macfixit.com/ultimate/Forum35/HTML/002421.html">this forum</a> at MacFixIt for some additional clarification on the code above.



[ Reply to This | # ]
More info on swap moves
Authored by: robg on Apr 03, '01 01:16:50AM
GDIF has written a how-to on moving swap to another drive; you can find it on his home page: http://homepage.mac.com/gdif/ -rob.

[ Reply to This | # ]
How to tell...
Authored by: robg on Apr 12, '01 12:11:48AM

You can tell if it worked by looking for three files:

swapfile0, swapfile1, and swapfile2

They normally live in /var/vm, and you can search for them by doing:

sudo find / -name "swapfile*"

The resulting list should show clearly where your swap files are currently residing.

-rob.



[ Reply to This | # ]
Privileges get messed up
Authored by: jgmanning on Apr 22, '01 01:51:56PM

I tried bombich's method and moved the swap file to the disk that had my Classic boot folder. Upon restarting Classic wouldn't lauch because I didn't have the proper privileges. I did a Get Info on that volume and checked "Ignore privileges" and restarted. Classic would now launch, but the apps still wouldn't run right, e.g., FMPro said I didn't have edit rights to any of my databases.

It appears that the swap volume is being mounted by root, so the admin user doesn't have the privileges required to use Classic. I eventually got it to work by using GDIF's method at

http://homepage.mac.com/gdif/ (thanks again, Rob)



[ Reply to This | # ]
Privileges get messed up
Authored by: bombich on Apr 23, '01 12:36:49AM

The swap file is always mounted as root -- no one else has had a chance to login when the swapfile is created. I'm not sure why your priveleges were messed up -- I can't imagine it had anything to do with the swapfile because priveleges for the swapfile shouldn't have changed. The method worked great for me.



[ Reply to This | # ]
Privileges get messed up
Authored by: jgmanning on Apr 23, '01 01:46:43PM

> The method worked great for me.

Do you have anything else on the swap volume?



[ Reply to This | # ]
Other stuff on swap?
Authored by: bombich on Apr 23, '01 10:00:00PM

Do I have anything else on the swap volume? No way dude, I played with fire once -- I'm giving X as much swap space as it wants.



[ Reply to This | # ]
Other stuff on swap?
Authored by: jgmanning on Apr 24, '01 04:19:26PM

That might be why you didn't notice privilege problems.
Or it could just be my bad karma.
If I had a spare partition I'd devote it to swap, too.



[ Reply to This | # ]
Swap file partition size
Authored by: Charlie Franklin on May 05, '01 04:06:49AM

What is the ideal size for a seperate swap file partition, of if there is no ideal size for all situations, what are the rules/guidelines for working out the best size?



[ Reply to This | # ]
Confused? swap partitions huh!?
Authored by: lerici on May 13, '01 06:32:48PM

Okay swappees what's cookin?

I've read a number of these "swap partition" articles and i guess I am confused.
The strategy seems to involve setting it up using the /etc/rc resource configuration
script. But I would think that the right way would actually be to set aside a separate
ufs partition with the formatter and then setting the swap area in /etc/fstab...

Here is my problem:

The manpage indicates that there should be only one /etc/fstab Mac OS X must be
far advanced beyond berkeley.. it has three.. LOL

fstab.hd:
/dev/hd0a / ufs rw 1 1
/dev/hd0e /var ufs rw 1 2
/dev/hd0f /usr ufs rw 1 2
/dev/hd0d /tmp mfs rw,-s=12000,-b=8192,-f=1024,-T=sd660 0 0

fstab.rd:
/dev/rd0a / ufs rw 1 1
/dev/rd0e /usr ufs rw 1 2
/dev/rd0f /var ufs rw 1 2
/dev/rd0d /tmp mfs rw,-s=12000,-b=8192,-f=1024,-T=rd335 0 0

fstab.sd
/dev/sd0a / ufs rw 1 1
/dev/sd0e /var ufs rw 1 2
/dev/sd0f /usr ufs rw 1 2
/dev/sd0d /tmp mfs rw,-s=12000,-b=8192,-f=1024,-T=sd660 0 0

Oh in case you are wondering about /tmp.. I guess an mfs is a memory file system. I suppose that this means it may, i repeat *may*, indicate that it is really a tmpfs which is typically created within the swap partition and files.. yep.. that may mean you need extra space within your swap partition to allow for heavy use of the /tmp directory. In UNIX typically every application drops its temporary files in that directory.. but OS X is a distant relative and may have lots of locations for temporary files.. who knows?

Okay, how would you typically set up the swap partition if you were to use /etc/fstab. Okay, let's look at the manpage. (oh btw Apple has informed that they do not, repeat do not support the UNIX OS only the GUI. So, until Apple developers have some free time to interact with the faithful..)

oh, the manpage: fstab sez that yes indeed there is a "swap" type partition which means that /etc/fstab may be used! There is even a "union" or translucent type partition.. this one
is scary don't ask.. it is something SUN contrived and later dropped. LOL

so how would you configure your swap partition?

okay.. well we have 3 fstab files? btw, this seems extremely silly. there is no documentation on why this would be but it adds a level of complexity to managing file systems which may indicate a poor design.

well alright let's look and see what those files say.. hmm how can you put things in fstab which df does not see.. that means they aren't mountable.. what are they doing in fstab? This seems like a bug to me.. but it could be that I just don't understand??????

Would some kind soul enlighten me? I am tempted to "hack" my way through this sans documentation but I am liking what I have so far under MAC OS X and I am afraid I might break something.. LOL.. BUT it would be nice if it really behaved like UNIX?

Thanks in advance,
Peter



[ Reply to This | # ]
Confused? swap partitions huh!?
Authored by: patpro on May 25, '01 07:29:05AM

for further info you should take a look at http://www.peak.org/~luomat/articles/FAQs/Swapfile_Swapdisk/index.html

As OSX swap mechanism is inherited from NeXT's one, it's of great interest to read these docs.



[ Reply to This | # ]
Confused? swap partitions huh!?
Authored by: Earl on Feb 08, '02 07:31:25AM

Correct.

Also, go to http://www.darwinfo.org and search the developers and users forums there for fstab and swapfile.

One of the developers plainly states that fstab does not do what you might think it does in Darwin, and explains why the methods involving changes to /etc/rc - while not what they originally had in mind - are AOK.

The idea of just specifying it in fstab would basically work for any of the other Unices I've seen - wait - they all do that but Darwin! :)



[ Reply to This | # ]
Confused? swap partitions huh!?
Authored by: Earl on Feb 08, '02 07:37:45AM

It's also important to stress that Darwin does not support swap partitions, only swapfiles. Also, Darwin's swapfiles are managed dynamically, so they can grow or shrink as needed - in about 80MB chunks.

The man pages might work for application level stuff, but for system's stuff, go to the Apple developer pages and the darwinfo site. As is often the case, the man pages aren't keeping up. (And I don't knock Apple for this. The heralded Linux - which I also use and like - is rife with "note this manpage is no longer being maintained...")



[ Reply to This | # ]
This does work!
Authored by: avkills on May 22, '01 03:29:59AM

Thanks guys, for posting this information.

I shuffled some stuff around and re-formatted my 18gb cheetah with a 1gb UFS partition for swap, plus two others for other high-speed needs.

I even went so far to remove the old swap directory off the system drive...re-booted and did the search, and it only came up with the new swap! So I know this is working. You can also do a "top" command in the terminal to verify VM is being written out, I believe.

-mark b



[ Reply to This | # ]
Swapfile Relocation Tutorial: Simple
Authored by: bombich on Jun 19, '01 04:15:47PM
Check out my Swapfile Relocation tutorial for a quick and simple method to move swapfiles to a separate partition.

[ Reply to This | # ]
Swapfile Relocation Tutorial: Simple
Authored by: patpro on Jun 20, '01 03:04:45AM

You may want to check your URL, it needs a '/' between '.edu' and '~'
This method was previoulsy described on Resexcellence.com (http://www.Resexcellence.com/hack_html_01/06-01-01.shtml)



[ Reply to This | # ]
Swapfile Relocation Tutorial: Simple
Authored by: bombich on Jul 13, '01 10:31:03AM
Tutorial is here (corrected URL)

[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: jtk on Nov 07, '01 12:51:10AM

Has anyone else noticed that X (v10.1) seems to rebuild the /dev directory at reboot? This is causing some odd behavior with the swap partition, since sometimes the device is /dev/disk0s9, sometimes it's /dev/disk1s9. I don't think I've ever seen another Un*x that does anything like this without being asked.

I haven't played around with it enough to know if there is a way to predict what device it will be assigned to on any given boot, but it is a potential "gotcha" when using these instructions.



[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: stomper on Nov 07, '01 03:54:14PM

Me too. I've moved my swap to another partition (/Volumes/Swap) and It works fine for two or three reboots. Then all of a sudden one of my total 8 volumes (4+4 on 2 disks) mount as "/Volumes/Swap 1", the swap file is created on that volume and the rest gets messed up. For now I've reverted back to default again until further light can be shed on this issue.



[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: dsauve on Nov 07, '01 08:04:23PM

I moved mine last weekend and haven't noticed any problems. I did use niload to enter the mounts in Netinfo Manager ... ie

sudo niload -m fstab / < /etc/fstab

There's man pages for "niload" ... I wonder if making NI aware of the mounts solves the problem?



[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: jtk on Nov 14, '01 06:53:17AM

Interesting idea. I had planned on giving it a try, but it looks like the 10.1.1 update automagically fixed the odd behavior on my system. <shrug>

If it happens again, I'll try your fix.



[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: dsauve on Nov 14, '01 07:36:55PM

This morning I rebooted remotely using 'shutdown' and one of my partitions didn't mount. This is the first time I saw this happen. I did a 2nd shutdown and all the mounts booted up fine. I wonder if 'shutdown' is the culprit -- if the reboot process isn't the same as a reboot after selecting Restart from the apple menu.

I only question the command because I noticed that my Display settings are screwed up after a 'shutdown' -- implying that something isn't correctly loaded -- and it requires a clean reboot to get the Displays to show the correct refresh rates, etc.

Point is, the niload trick isn't perfect.

-damon



[ Reply to This | # ]
/dev Rebuilt During Boot?
Authored by: jtk on Nov 22, '01 01:14:32AM

If /sbin/shutdown is the culprit, then we're back to the "OS X's automounter does funny things with /dev" line again. Maybe someone who follows the Darwin groups, has some free time, and wouldn't mind could float the question and report back...?



[ Reply to This | # ]
swap partitiion on same disk as X
Authored by: jackg on Dec 09, '01 05:14:57PM

I saw an article about how to create a partition on the same disk that X is installed on. The swapfile, now a disk, would appear on the desktop and the gentleman working on it was next going to figure out how to remove it from the desktop. Does anybody know the article I'm talking about---I'ld like to implement this.

Thanks
John



[ Reply to This | # ]
swap partitiion on same disk as X
Authored by: Earl on Feb 08, '02 07:42:42AM

This method - creating a disk image on your OS X partition - was discussed at length on the darwinfo site, and the bottom line - from the developers - is don't even think about using this method. It's highly inefficient, and failure prone.

http://www.darwinfo.org and then check our the developer and user forums for this (search on swapfile, fstab, virtual memory, that sort of thing).



[ Reply to This | # ]
Fails after reboot
Authored by: gisborne on Dec 27, '01 03:46:26PM

This works after the first reboot, but after I reboot again, my other volume is mounted as Swap 1, and I have a Swap directory on my main volume.

I've tried both /Volumes/Swap (which it seems to me is what these directions ought to say) and /Swap, and I get the same thing every time).

Suggestions?



[ Reply to This | # ]
Fastest Swap is NO swap
Authored by: aricart on Jan 01, '02 03:36:27PM

MacOS X and its predecessor NEXTSTEP share very similar performance characteristics when the amount of memory is not adequate: they system thrashes and becomes more and more unusable. Switching between apps takes a long time and the computer cannot keep up at all. While moving the swap file can and does help a little, this is just a band-aid to the real problem: a lack of memory. Tricks like moving a swap file are fine for server type applications. On a user environment a few dollars can make a bigger difference.

The real fix is really to add more memory. Lets face it for $89 you can buy a 512MB DIMM and even low entry iMacs can now take one full GB. Seems to me that best investment one could make includes maxing out the memory on the machine as much as you can afford. A 512MB or 1GB machine will be a very different animal. The swap drive under normal conditions will not really be used and things will run very differently (and fast).

What does a 1GB Mac OS X feel like? They way it should.



[ Reply to This | # ]
Tool to make it easy
Authored by: colinlai on Feb 08, '02 02:41:00AM

A freeware called "Swap Cop" can make this really easy. Just click a few buttons and it's done, though some users reported problems.

http://www.versiontracker.com/moreinfo.fcgi?id=13514&db=macosx



[ Reply to This | # ]
Tool to make it easy
Authored by: 128K Mac on Feb 08, '02 06:15:28PM

Swap Cop by J. Schrier does seem to be the way to go especially for long time Mac heads who don't like to use Terminal. Hate to ruin all the fun of the geeks. ;)

I'd also endorse, however, the earlier idea of aricart regarding RAM. Have 1.5GB in my tower and 1.0GB in tibook. They're putting dimms in cracker jack boxes these days, it's so cheap, and the investment with OS X has a remarkable effect in many cases. Always necessary? Of course not. 256MB or 384MB works.

But 512MB works better and 1.0GB better and......



[ Reply to This | # ]
Tool to make it easy
Authored by: tazmandevil on Sep 29, '02 06:10:49AM

There's no insurance that this works under 10.2 Jaguar from the Author! And in fact, it does not work!



[ Reply to This | # ]