top of page
acziruppada

Disable Swap Files In OS X: Tips and Tricks for Advanced Users



I suppose people would do it for an SSD drive because those drives don't have the same lifetime as standard magnetic spinning-disk drives. There are a more limited number of write cycles, so presumably using them with a swap file would use up these cycles more quickly.




Disable Swap Files In OS X



Also, wired memory isn't released when it becomes invalid, it is only released when a page out event is triggered, which won't happen if Virtual Memory is disabled. So it won't take long for all your memory to be gobbled up even if it is no longer used.


The reason they disabled the swap file is likely paranoia about SSDs having a maximum number of writes per block. Swap files can be written to frequently, possibly causing disk fragmentation (as well as file system fragmentation) and, eventually, possibly causing the disk to fail sooner.


That said, this is definitely a case of premature optimization. It is almost always a HORRIBLE, HORRIBLE idea to disable your swap file. The swap file is there for when your running programs consume more memory than you have physically installed. If you disable it, you may not be able to run as many programs at the same time (and the limit is far lower than you'd expect) or you may have issues (like freezing and having to hard-reboot the machine) when you run out of memory.


The latter I've experienced (with the swap file enabled) when running out of disk space. I had a 120 gig drive with 3 gigs free. While playing Civilization IV (a large, full-screen game), the system ran out of hard drive space (50 megs free) and presented the "kill apps now" window. This was hiding behind the full-screen game, so the system just locked up. If I hadn't been able to ssh in from another Mac to kill several apps remotely, I'd have had to hold down the power button.


The reason you can disable swap is so the system can run on read only media such as a CD, DVD, locked network image using NetBoot or a read only USB fob like the new Lion installer or recovery fob that ships with some macs that lack optical drives.


Disabling swap prevents the mac from using a tiny portion of your drive as a cache for some memory that has been calculated but the program wasn't smart enough to write those results itself to a proper cache.


I would keep my swap on even if I knew for certain I had to pay for a new drive annually. The speed up that swap gives me is HUGE. This is not always so, and at times when the system is thrashing or paging - you can have the opposite. Many people choose to run without swap so that they get a memory allocation error rather than let their mac start paging since that can slow down a machine when things aren't running smoothly.


My experience is similar to w00t's. I have disabled the swap file on SL and Lion because the performance increase works for me. Wired usage does get out of control after a week or so but a simple reboot fixes that. I do not have a SSD but a slow 5400rpm disk. SSD lifespans aren't actually as bad as people try to make them out to be though. If you're doing it simply because you're afraid of wearing out your SSD please see: -vertex-3-pro-preview-the-first-sf2500-ssd/2 and =0 Intel rates their drives for over 20GB a day, every day, for up to 5 years on a 80gb drive. Bigger sized drives will last even longer. The average user doesn't even use 10GB/day.


I make sure to monitor my 4GB memory usage with iStat Menus and use an Automator script with the "purge" bash command (requires Xcode to be installed) to clear out inactive memory and open up more free memory frequently. The few times I have had apps with memory leaks use up all the RAM, I did have to do a hard reset. The rare occasions of that happening and the speed up in general makes it worthwhile to keep the swap file disabled for me. I realize that I can't have as many apps open at once because of this as stated by others but I'm able to work with that.


The downside risk: if your battery dies while your laptop is sleeping, your current state will be lost. This will be equivalent to a forced hard restart. No physical harm to the laptop, just a loss of productivity. If you disable hibernation, then run your battery low and can't charge, just have the code handy to enable hibernation!


Disabling swap does increase performance. For example I regularly use Aperture and when swap grows performance worsens and when it goes beyond 1Gb I have to restart OS to clean it and restore performance. Note: machine has 6GB of RAM and NOT all of it occupied, about 3GB is free, but 1G is swap significantly degrades performance.


If one is a user who routinely runs within 5 GB of a full drive, disabling swap will stop the constant nagging of running out of space; I routinely wind up with a 4+ GB swapfile from routine browsing.


The speed improvement is astounding on older machines; it's not that impressive on SSD equipped modern machines. (Under OSX 10.0 on an iMac, using virtual memory halved the speed of many operations, including copying files between drives).


If you are low in available physical RAM, and on today's machine, I would say if you have 4 GB or less RAM. Then you should probably have a swap. But this depends on your workload and how you use your machine.


If you have more than 4GB of RAM, there is a good chance that your system might never use the swap. So if the swap is enable or not, this won't change much.Although this depends on other factors, the longer your machine will be running the more likely the OS will swap small unused portion of the RAM to the swap eventhough there is plenty of place left apparently. However, often the system use the "free" RAM for file buffers and other stuff, and some workload might make your file buffer use all available RAM.


Any way what would happen if you disable the swap and your memory is completely used? Well, the next time an application request more memory (like with a malloc or realloc), it will receive an error! If it is properly coded, the application will detect it, but handling such an error and terminating the program gracefully could be a challenge still! However, many developer don't test for successful memory allocation and often the application crashes as soon as this non allocated memory is going to be used. So there is a strong chance that the application will crash, and actually that many application will crash because you have more than one application usually running at the same time. Hopefully, the kernel itself should be immune, but in my experience it was not always the case.


I have personnaly 2 machines with 8 and 16 GB of RAM. My personal usage of these machine should never bring me close to use all of this RAM (today! perhaps in a few years that could be different). The only reason I would hit the physical RAM limits is that a process got rogue and is consuming all the RAM. Having no swap is a relief, the kernel will promptly kill the rogue process as soon as it hit the RAM limit. If you have swap, you will get a painful experience (maybe not on SSD...) as your system will swap and swap and swap and become sometimes totally unresponsive until hopefully your kernel kills the culprit!


You should not be scared by people telling you something horribly wrong might happen if you remove swap. If you do so, your physical address space will be reduced, much like if you would go from 4 GB of RAM to 2 GB. Would you do such a thing? Then you should not remove the swap. You can however decide to limit it, and that would be wise.


When swap should be kept, a user should try to set limits to the amount of memory a process can use. On a Linux system, one could use cgroups memory limits to avoid that some process gets out of control, I don't know the equivalent on OS X but that should be the way.


However, if one has more than enough available memory for his workload, then he could safely disable swap or he could keep it. It won't change much even in terms of performance because if the system does not use the swap, then you don't get hit by a performance drop (especially visible on an old rotational hard disk, less so on SSD).


I have an old PC with 1 GB RAM, there I do have a big swap: 4 GB so I can run Firefox and other application on it. I tend to use as little application (or tabs in Firefox) as possible to have a correct user experience though.


I have a small server with 8 GB RAM. There I know exactly my work load and I do not want my server to slow down because a process got rogue and is consuming all memory. There I have no swap and cgroup memory limit enforcing barrier, so applications or services can crash but not the kernel. Btw, this is a Linux machine.


Then, I have my Mac with 16 GB RAM. I am not the sole user of it, and we would not mind waiting when switching user. There we do have swap, it is difficult for me to plan the work load of other users of this computer. Even if I would be the only one I would use swap. I often run several VMs and use memory consuming applications, but not all at he same time, so I would not mind the OS swaping in an unused app so I can get faster disk I/O or launch yet another VM. Althoug up to now, I haven't seen this machine swapping!


I just disabled the swap file on my 2011 Macbook air (4GB RAM, 128GB SSD) because I was sick of the swap file slowly eating up all my remaining HD space and then having to restart. This way, the idea is that I will just run less programs.


Unfortunately, if the SSD is used as system disk with limited RAM, it will be worn out rapidly by the swap mechanism. For example, my MacBook with new SSD has powered on for 161 hours, and it has been written for 663GB data(!). To tackle this issue, I should either upgrade my MacBook ($) or try to limit the usage of memory by disabling swap and doing minor tweaks.


To check the current mode that system manages memory, type sysctl -a vm.compressor_mode in terminal. The default mode is 4, which represents compressing memory and using swap. To disable swap, we should change the mode from 4 to 2, which means compressing memory only. 2ff7e9595c


1 view0 comments

Recent Posts

See All

Comments


  • White Facebook Icon
  • White Instagram Icon
  • White Twitter Icon
  • White YouTube Icon
bottom of page