VHD Disk Compaction

VHD Disk Compaction is a process that runs every time a user signs out. The compaction process is designed to automatically decrease the amount of storage a user's container, depending on a predefined threshold. During the sign out phase, FSLogix evaluates the disk to determine if disk compaction should run.

Note

VHD Disk Compaction affects Profile and ODFC containers (including Cloud Cache configurations)

VHD Disk Compaction is Enabled by default. If you want to disable it, see Disable VHD Disk Compaction.

The compaction process relies on the Optimize Drives service (defragsvc). This service determines the minimum supported size by querying the MSFT_Partition API, using the SizeMin parameter from the GetSupportedSize method. This operation takes into account the location of immovable files (that is, files that can't be moved). For more information, see GetSupportedSize method of the MSFT_Partition class.

Important

  • If the StartupType of the Optimize Drives service (defragsvc) is set to Disabled, VHD Disk Compaction will fail to query the minimum supported size and the process will not run. The service StartupType must be set to Manual or Automatic regardless of whether the service status is Running or Stopped.

  • You must use dynamically expanding virtual hard disks; fixed size virtual hard disks cannot be compacted.

Determining when VHD Disk Compaction runs

FSLogix determines whether to run the compaction process each time a user signs out. If the following criteria are met, VHD Disk Compaction runs:

  • Size of the container must be greater than 1 GB (size on disk).
  • Difference(C) between the consumed size on disk(A) and the queried minimum size(B). The difference must be greater than or equal to 20% of the consumed size within the container(A).

The difference is calculated as follows:

  • A - B = C
    • If C ≥ (A x 0.2), compaction runs.
    • If C < (A x 0.2), compaction doesn't run.

Example scenarios

Here are a couple of examples:

  • Example 1: The consumed size of a container is 6GB. When we call GetSupportedSize, the resulting minimum size is 2.5GB:

    • 6 - 2.5 = 3.5 GB
    • 20% of 6 GB = 1.2 GB
    • 3.5 GB is greater than 1.2 GB, compaction runs.
  • Example 2: The consumed size of a container is 12GB. When we call GetSupportedSize, the resulting minimum size is 10GB:

    • 12 - 10 = 2 GB
    • 20% of 12 GB = 2.4 GB
    • 2 GB is less than 2.4 GB, compaction doesn't run.

Example table:

Container Fixed/Dynamic Configured Size Consumed Size Supported Size Will Compact?
profile_<username>.vhdx Dynamic 30 GB 6 GB 2.5 GB Yes
profile_<username>.vhdx Dynamic 30 GB 12 GB 10 GB No
profile_<username>.vhdx Fixed 30 GB 30 GB N/A No

User experience

When compaction runs, users see the message Waiting for FSLogix Apps Services for some time. Depending on the space to recover, the compaction process should take an average of 20 – 30 seconds. The first compact operation is the longest and is based on recoverable space and other compute resource factors.

Each subsequent sign out should be quicker as the amount of data to compact should decrease. VHD Disk Compaction can run for a maximum of 5 minutes. If this maximum is reached, the operation ends, and the sign out continues. During the next sign out, the operation will resume from the previous state.

Winlogon events

VHD Disk Compaction operates at user sign out, which is part of the Winlogon process. The Winlogon process creates a warning event anytime a process causes the sign out time to exceed 60 seconds. These warnings are normal and expected. The compaction process could exceed 60 seconds depending on the size of the VHD(x) and the space to be recovered. The Winlogon threshold is a fixed value and can't be changed.

See Winlogon (sign out) Delay Warnings for a Log Analytics query to show the number of operations that exceed the 60-second threshold and their durations.

Next steps