Release unused space from your Windows Azure Virtual Hard Disks to reduce their billable size

A few weeks ago Mike Wood talked about the TRIM support for Windows Azure Virtual Machines: TRIM Support comes to Windows Azure Virtual Machines. In his post Mike explains how a feature similar to TRIM for SSDs is implemented in Windows Azure Virtual Machines on the host side, to effectively reduce the actual (billable) size of your Virtual Hard Disks. And he also explains that we can take full advantage of this feature with Virtual Machines running on Windows Server 2012 or Windows Server 2012 R2 (support for 2008 R2 is limited to the OS Disk).

In this post we’ll be looking at how this TRIM feature actually works and how we can schedule it to run daily or run it manually with PowerShell.

Our test case

I have a Virtual Machine running Windows Server 2012 R2 on which I’ll start by attaching an empty disk (50 GB):

Now yesterday I wrote a little post about calculating the actual size of a VHD, what a coincidence :) With this tool I can see the actual size of this new disk, which is only a few bytes large and not the full 50 GB. This is what we expected since the VHDs are stored in a sparse format.

The next thing we’ll do is fill the disk with random data to continue with a few tests. The first little script I stumbled upon was the “Build 2013 Sessions Download” script by Sahil Malik, which I downloaded on the VM to finally execute it on the new data disk. After running this for a few minutes I had already downloaded 14 files with a total of almost 5 GB. This also showed in the VHD Size tool:

Now for the actual test, let’s delete all the files we just downloaded. If I look at the disk in the VM it’s empty, but the VHD size tool tells me the actual/billable size of the VHD hasn’t changed a bit. This is because removed files are not “released” automatically.

If you read Mike’s post you’ll know that something happens behind the scenes to eventually release the files. We’ll get back to that soon.

TRIM-diddly-doo

Now that Virtual Machines support something similar to TRIM for SSD drives you can now manually request to release the unused space from the disk. Let’s start with some REAL simple PowerShell:

Optimize-Volume -DriveLetter F -ReTrim

And… that’s it, the unused space on my F drive has been released. After running the VHD size tool again you can see the result: the VHD went from 4.87 GB to 87.1 MB actual size.

Besides the PowerShell cmdlet you can also access this feature through the updated defrag tool: Defragment and Optimize Drives

By default all drives are optimized weekly, meaning the space will eventually be released once a week. The Change settings button allows you to change the schedule (to daily, which could be useful if  the size of your data increases and decreases regularly) and the Optimize button allows you to manually release the space for the selected disk, similar to the PowerShell Cmdlet.

Considerations

For production usage the weekly optimizations will probably be fine and you’ll get weekly optimizations out of the box. But as soon as you turn of your Virtual Machine (maybe you only need to run the VM during office hours, or once a week, once a month, …) you might want to manually trim the drive before shutting down the VM. By doing so you’ll reduce the size of your VHD to its optimal size before you put the machine to sleep, which might have a positive effect on your monthly bill (or like Scott Hanselman likes to say it, Penny Pinching in the Cloud).

Sandrino

About Sandrino Di Mattia

Sandrino Di Mattia is a Windows Azure Consultant at RealDolmen and a Windows Azure Insider. He lives and breathes Windows Azure.