← Back to overview
December 17, 2013 · Disks Page Blob Size

Calculating how much storage space a Windows Azure Virtual Hard Disk is really using

When working with Virtual Machines in Windows Azure, the disks (VHDs) are persisted as Page Blobs in your Storage Account (excepted for the temporary disk). These page blobs are stored in sparse format which means you'll only pay for the data you've actually written to the disk. Let's say you have a 50 GB disk on which you uploaded 1 GB of files, you'll be paying for that 1 GB of data and not for the whole 50 GB.

Note: When you delete files from the Virtual Hard Disk, the free space is not immediately reclaimed. Starting from Windows Server 2012 this will eventually happen on a specific interval. More information: TRIM Support comes to Windows Azure Virtual Machines and Release unused space from your Windows Azure Virtual Hard Disks to reduce their billable size

Now today I was working on something where I needed to find the actual size of the disk and not the "logical size".Neil Mackenzie pointed me to a post by the Storage Teamwhich explains how you can iterate over the pages in a Page Blobto calculate the complete size of the blob. And a few months ago the team also published a script to calculate the Billable Size of a Blob or a Container:Get Billable Size of Windows Azure Blobs (w/Snapshots) in a Container or Account.

Based on this script I created a small extension method which makes it possible to calculate the actual size of a VHD:

public static long GetActualDiskSize(this CloudPageBlob pageBlob)  
{
    pageBlob.FetchAttributes();
    return 124 + pageBlob.Name.Length * 2
       + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3)
       + pageBlob.GetPageRanges().Sum(r => 12 + (r.EndOffset - r.StartOffset));
}

[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
public static extern long StrFormatByteSize(long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize);

public static string GetFormattedDiskSize(long size)  
{
    var sb = new StringBuilder(11);
    StrFormatByteSize(size, sb, sb.Capacity);
    return sb.ToString();
}

Based on this bit of code I created a Console Application which allows you to get the actual size of a Page Blob or to get the actual size of each Page Blob in a container:

As you can see I created a 50 GB data disk, but since I only have a few files on it the actual (billable) size of the disk is 1.14 GB.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus