Automating VM Memory (un)Locking


With VDI it’s always a best practice lock VM memory and avoid over-subscription to keep away from host swapping, etc.

When I’m doing my benchmarking I’m constantly trying to see how many VMs I can push out  before I hit a CPU or IO bottleneck.  As part of that I test with both locked and not-locked VM memory.  (For those unaware with locked VM memory, it essentially reserves all memory for a VM and eliminates .vswp)

Here’s some PowerCLI to automate this:

Locking VM Memory

Un-locking VM Memory

  • Sergio Oliva

    Just a comment for unlocking part: it’s true that “all guest memory locked” is unchecked but memory reservation is still fixed to the amount of memory configured on VM.

    If you want to really unlock memory you should include following:

    $currentvm | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationGB 0 -Confirm:$false

    • stevenpoitras

      Great comment! For VMs with prior reservations this would be necessary to remove the reservation as well as unlock the memory

    • stevenpoitras

      Great comment! Updated to include this for the un-locking using the VM’s View:

      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $spec.memoryReservationLockedToMax = $false
      $spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo
      $spec.memoryAllocation.Reservation = 0

Legal Mumbo Jumbo

Copyright © Steven Poitras, The Nutanix Bible and, 2014. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Steven Poitras and with appropriate and specific direction to the original content.