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.
-
Something will be leaking memory- you may be able to use this info to find out what. quora.com/…– DanackMar 9, 2013 at 1:17
-
Well, what if it's the window server that takes up 4GB of virtual memory? And I don't think you can really fix a memory leak in another app right.. Guess I'll have to restart it, thanks anyway.– theAmateurProgrammerMar 9, 2013 at 3:15
-
How to restart just WindowServer on OSX hints.macworld.com/article.php?story=20071121154033749– DanackMar 9, 2013 at 4:35
4 Answers
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.
Swap
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
-
-
-
1Under El Capitan, trying to clear swap is not permitted by the new System Integrity Protection.
/System/Library/LaunchDaemons/com.apple.dynamic_pager.plist: Operation not permitted while System Integrity Protection is engaged
– leonsasMar 6, 2016 at 17:05
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 :)
-
1
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!
-
Thanks! It's funny, because I've had all of my apps crashed because my laptop ran out of space with 100 mb free, but 2 minutes later I have 8 GB free. Will do a backup first, and then try to delete the swap files. Mar 12, 2013 at 11:56
-
2The swap files contain data that is not currently used in real memory but when that data is needed this will be swapped back into real memory. Swap files should not be deleted they are not caches that will be recreated/– mmmmmmApr 17, 2013 at 18:40
-
@wwasd, suppose they are outputs/caches that will be recreated, what is the process of recreation?– PacerierApr 23, 2020 at 19:38