So I've got a 13" Macbook Air with 128GB, and recently I got a message showing that I'm running out of space, so I did a quick check to see what was taking up all of space, and I found out that I have 4GB of sleep image (which is normal), but 20 swap files, each taking up from 60mb to 1gb. With a small disk size, this is really unacceptable so I was wondering if it was safe to delete some of the swap files and let the system build them up. Or would a restart help (Though I'd prefer not, had a bet with my friend to keep my uptime as long as possible :p) Any suggestions appreciated.
You can disable Hibernate to regain your size in RAM on your SSD/HD: First disable it:
sudo pmset -a hibernatemode 0
Then free up space:
sudo rm /var/vm/sleepimage
... if you want to enable it again:
sudo pmset -a hibernatemode 3
Note: -a means on charger and on battery, -b means battery and -c means with charger.
Alternatively if you have more than one disk you can symlink sleepimage to an HD so it can physically locate there.
First disable Swap:
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
Later delete the files:
sudo rm /private/var/vm/swapfile*
... if you want to enable it again do:
sudo launchctl load -wF /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist
A restart will definitively solve the issue.
Even if you kill the offending process, the system won't clear up the swap files - you need a reboot to do that.
Also, deleting the swap files would be a bad idea. You would need to sudo to do it, and if the system has the file open, even if you rm the file, it'll still be kept around as the file handle would be open, so you wouldn't gain the space back.
By all means try it, but it's not recommended :)
Another answer already mentioned disabling hibernate, but per the man page for
pmset, this is no longer enough to disable the hibernation image (sleepimage) file creation.
The docs say,
To disable hibernation images completely, ensure hibernatemode standby and autopoweroff are all set to 0.
So we need:
sudo pmset -a hibernatemode 0 sudo pmset -a standby 0 sudo pmset -a autopoweroff 0
You can verify your settings before and after the change using
sudo pmset -g. Then you can
sudo rm /var/vm/sleepimage to delete the file, but in my case this was done automatically after setting all 3 values.
You can delete the sleepimage without damaging anything. The system will recreate it over time. Usually when I get a message that I'm running out of space I just go to the terminal and type
sudo rm /var/vm/sleepimage and I get about 9 gigs back but the system will rebuild it over time, so it's only ever a temporary reprieve.
To finish answering your question, and to let you know why you should trust me, you can delete every file in
var/vm because they are caches of what is stored in your RAM currently. They are not critical... unless you lose power, and in a restart, most of the
var folder is cleared automatically. Though, to provide users with steady reporting of their free space, OS X Lion and newer do not delete the older
vm files at startup.
Basically, you'll always have VM files with size roughly equivalent to the amount of RAM you have used running programs. You can delete these and upon the next significant background activity event, the computer may re-create them. If you only have one program running, and doing nothing, but have huge VM files, that may be a sign of a memory leak in a program, but I have noticed that a lot of the preference panels and plugins on the menu bar are really huge memory hogs: If you run the Google Drive Synchronizer thing and the Dropbox app and the box.com app then those will call home 24/7 and move files around non-stop. They are more than enough activity to generate legitimate and huge VM profiles.
Hope that helps!