After running your virtual machine for some time you probably have created, deleted and modified a great deal of files. And let’s not forget the operating system which constantly modifies files. This will take up space on your host operating system, even if these files should be deleted.
Some virtualization softwares do support reclaiming disk space out-of-the-box. VM Ware Fusion for Mac is one example of such software. And most of the enterprise solutions also support this. However, should you be using other tools that do not have this option, such as VirtualBox, there is still some ways to achieve this.
Continue to read more about how free space is managed in virtual machine guest os:es or skip directly to the solution.
First a little bit of background (to help you understand)
Without digging into the details how virtualization and hard drives work, this constant changing of files creates gaps between the actual files. Maybe you remember the days with mechanical disks and constant disk (de)fragmentation. On an traditional computer (whether it has a SSD or a HDD) this poses no actual disk space problem as the operating system will reuse the gaps (as these are treated as free space). However, as hard disks in a virtual machine actually is one large file, this might become a problem.
Consider this example. You start with a 20 GB virtual hard disk with 10 GB of free space. Then you save one 5 GB file (and thus have 5 GB free space left). After this you store another file, this time one that is mere 1 MB, then you would have 5GB of free space on the guest operating system. Should you take a look at the virtual hard disk file on your host os you will find that the file is about 15 GB large. The you switch back to your guest os again and deletes the 5 GB file. Now you should have reclaimed 5 GB and have 10 GB of free space, right? For the guest os that is true. However, taking a look att the host os you will see that the virtual disk file is still 15 GB.
Reclaim your space (especially important on Windows guest os:es)
There are some tricks you can use to reclaim your space.
- Begin with a disk defragmentation of your guest os.
- Then use a tool such as SDelete (from SysInternals, for Windows) to make sure a all “free” space is zeroed-out.
- Compact your virtual disk file. As there are multiple virtualization softwares out there I will present a couple of options for those of you using Vagrant and or Virtual Box. See below:
Option A: Using VM Ware Fusion:
If you are using the .vmdk format (VM Ware Virtual Disk) you could either:
- attach the disk to a new machine in WM Ware Fusion (should you be lucky enough to got a license), or
- bring to a friend with VM Ware Fusion
Option B: Using VirtualBox:
If you are using VirtualBox with other formats than .vmdk you could clone your current disk into the .vmdk-format. During this cloning process only the actual space needed will be used for the new file. From here on you could either choose to continue with this format or you could reverse this process (that is: delete original file, clone new .vmdk file to the format of your choice and finally delete the temporary .vmdk file).
Note: the actual free space needed on your host os would be the amount of occupied space reported by the guest os (and not the full amount of the virtual hard disk file).