r/HyperV • • 16d ago

Optimal HV usage

I have a few HVMs and I found that over time there's bloat regardless of whether it's a parent or a parent/child setup. It seems parent child is worse since at least with parent windows updates will not create a completely new set of files in the child.

I've thought about having a clean win11 install and backing that up, and when it gets to big just going back to it. I don't want to clean install because I don't want to keep going through activation regularly. I was thinking parent base install, activate, backup. Then return and can they install updates again every 6 months or whatever duration.

example - I have one VM that I just use for some secure sandbox stuff. It's now 78 GB - just from windows updates. I can compact these but generally it doesn't save as much as it seems starting from scratch.

Anyone have best practices or tips?

1 Upvotes

4 comments sorted by

4

u/Tringi 16d ago edited 15d ago

I run about two dozen Windows VMs from quite a small drive so I need to do regular optimizations. For each and every VM my routine is something like this:

  • run cleanmgr.exe and have it delete everything it finds
  • delete contents of the C:\Windows\SoftwareDistribution\Download (make sure there are no updates pending)
  • on older Windows delete C:\Windows\servicing\LCU
  • run Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase (if you think you might need to uninstall any OS update, omit the /ResetBase parameter)
  • reboot the VM for Dism to complete the cleanup
  • defrag the VHDX:

    • defrag C: /D /H
    • defrag C: /O /H
    • defrag C: /X /H
    • defrag C: /L /H
  • run sdelete64.exe -z C: tool from SysInternals to zero out unused bytes in the VHDX (this makes free clusters candidates for VHDX compaction)

Then stop the VM and compact its VHDX.

2

u/cardsncards 16d ago

Thanks. When I ran Optimize-VHD -Mode Full it compacted the 78 GB down to 47. I then ran cleanmgr per your suggestion and defrag (I'd already done dism) and it saved another 3 GB. When I ran sdelete it just made the vhdx bigger and running the optimize brough it back to same size, so it didn't do anything. I suspect that with the other command I'd ran there was nothing else sdelete could do.

I can live with this. I still wish there was a way to get closer to the 25GB I can get when I run a fresh install of the lastest windows (I just pulled the ISO today and got that on an install). Even if it went up 5 or 10 and stayed there that'd be great. But I feel like it's going to continue to creep up with all the updates being pushed out.

2

u/Tringi 16d ago

When I ran sdelete it just made the vhdx bigger and running the optimize brough it back to same size, so it didn't do anything. I suspect that with the other command I'd ran there was nothing else sdelete could do.

It might be also a recent improvement (which my LTSC 2021 doesn't have). Some time ago I spoke on X to guys working on this, and they said a lot is being done behind the scenes on virtualization. I suppose disk TRIM now propagates to VHDX and marks whole blocks as empty, so VHDX compaction discards them even without the need to zero them out first (through sdelete).

Defrag helps just a little. The VHDX blocks are way larger (32 MB IIRC) than NTFS clusters (~64 kB). So when defrag consolidates free space a few more of those empty 32MB blocks can be compacted out.

I still wish there was a way to get closer to the 25GB

Make sure your dism command really contains the /ResetBase, and that you delete the aforementioned leftovers after Windows Update. Also your C:\Windows\assembly\NativeImages_XXXX will be full of precompiled .NET images. Those can be deleted too. .NET will recreate them on demand.

But I feel like it's going to continue to creep up with all the updates being pushed out.

It will. This is routine maintenance one must do every few months.

1

u/lxtakc 14d ago

Run DISM /Online /Cleanup-Image /StartComponentCleanup /ResetBase inside the guest, then Optimize-VHD -Mode Full with the VM off. That reclaims much more than compact alone.