Yes, you can delete them. You can even tell your Mac to never swap again. But you shouldn’t. Even on systems that have a lot of memory, your Mac might find a need to swap to its storage space instead of use its primary memory, or RAM. There is no way to know what is in a given file, so it is unpredictable when a file will be deleted.
What is the Mac swapfile? Is it important? Can you delete it? We’ll walk you through this mysterious Mac file and what you can do about it.
If you’ve even run out of disk space on your Mac, you’ve probably sat and taken some time to look and see what’s eating up all this space (pro tip: it’s easy to forget how many files you move to the Mac Trash folder; the first thing you should do when you run out of space is right-click on the Trash icon on the dock and select Empty Trash).
You might have run across something called swap, or a swapfile. It can be difficult to understand just what these files are, and whether you can manage to do without them, especially since they seem to just sit there and take up space; sometimes they take up quite a lot of space indeed.
What is the Mac swapfile?
Before we delve into what the swapfile is, we have to talk about swapping in the context of how your computer works. When you run a program on your Mac, it gets loaded into your memory (RAM). You have a much smaller amount of RAM than you do storage on your SSD.
Occasionally, if you’re doing something that requires a lot of memory, you’ll come up against some of the limits that come with having limited RAM. Enter paging. Paging is what we call using your storage drive as memory. It’s all done automatically by your computer, so you never tend to notice when it happens. While the terms originally meant something different, these days paging and swapping are largely synonymous.
When you write something to your disk, it isn’t always written in contiguous stretches of storage; instead it might be written in a number of places, wherever your Mac (PCs do this, too) finds an open spot.
In order for swapping to work, your Mac usually needs one of those contiguous stretches, which can be difficult to find on a drive as it increasingly fills up with data. To mitigate this, OS X will generate a number of these swapfiles so that it can write (or page, or swap) to them whenever it needs them.
You can find them by navigating to an arcane folder deep within the bowels of your Mac. Just click on an open part of your desktop and mouse up to the bar at the top of your screen. Click on Go, and in the drop-down menu, click on Go to Folder.
A box will appear with an address bar in it; you’ll want to copy and paste the following location into it: /private/var/vm/ and hit enter. Finder will pop up with a new window listing the swapfiles your Mac currently has active.
How many files appear depend on a number of factors: how much and how often the Mac has needed to swap to your storage drive (which itself will depend on how much memory you have and how many programs you use that may have memory leaks). For reference, the above swapfiles were generated on a Macbook Pro with 16GB of RAM; it’s gone around ten days since it was last rebooted.
Can you delete Mac swapfiles?
Yes, you can delete them. You can even tell your Mac to never swap again. But you shouldn’t. Even on systems that have a lot of memory, your Mac might find a need to swap to its storage space instead of use its primary memory, or RAM.
If you delete your swapfiles, you might cause your system to crash, as it’s possible that your Mac is using one of them right as you delete it. The same goes for whether you should disable the ability for your Mac to use swapfiles in the first place – the best result is that you won’t notice a difference, and it’s more likely to make your Mac increasingly unstable.
If you really need to free up some space taken up by your Mac’s swapfiles, there’s an easy and simple fix: just reboot your Mac. Shut it down and restart it, and then check your swapfile directory again – they should either be gone or substantially reduced in size.
Chances are good that on a new Mac you’re unlikely to run into issues where your swap is seriously impacting how much free space you have. Should you keep running into an issue, however, take a look at the apps you run on a regular basis, and try playing around with them one at a time. You might find that one app has a memory leak, and by rebooting after use or finding an alternative app, you can avoid the big swapfile issue altogether!
Swap files are easily misunderstood. They will disappear when no longer needed, but they go away in reverse order, i.e., newest first. There is no way to know what is in a given file, so it is unpredictable when a file will be deleted.
Note that if an application allocates a lot of memory, and a new swap file is created, it is because the system is moving other stuff out of the way to make room in RAM for the active application. Active memory is always allocated in RAM. So if you quit that application, the swap file will generally not disappear right away. The swap space is being used for something else.
Once a swap file is created, it doesn’t move or change size until it is deleted. The system has direct access to the the internals of the file. From the point of view of the file system the file doesn’t change until it is deleted. The modification time will not change. It is meaningless for judging when the file was last used.
You can not delete a swap file. sudo rm does not delete the file. It “removes” the directory entry. In Unix terminology, it “unlinks” the file. As long as the file is in use it will not be deleted. That is why the disk space is not released. The file is still there and still in use. By removing the directory entry, you can no longer see the file. You can not monitor whether it is still there (except indirectly, by watching disk space). You, as a user, no longer have any link to the file. But the system does have a link to it, and the file can not be deleted until the system releases it. Various “cache cleaners” that claim to delete swap files are almost certainly bogus. No doubt they do the equivalent of sudo rm.
The mere existence of swap files has no effect on system performance. The performance hit only comes when memory is being moved in or out of RAM. If something in the swap file is needed, it is moved into RAM. There will be a temporary lag as the swapping takes place, but then the memory remains in RAM as long as it is active. If the disk space used by swap files is an issue, then you need more disk space, more RAM, or probably both.