Why does Disk Cleanup take so much time and cpu?

30

6

Just a curiosity question. Disk Cleanup has been spending 100% of one of my cores for quite a while and still going. Seems to be spending a lot of time especially on Windows Update Cleanup.

Can't quite understand why deleting files should be such an intensive activity. Especially since it already spent quite a lot of time calculating what it could delete.

Why does Disk Cleanup take so much time and cpu? What does it do that is so intensive?

Two of four cores maxed out

Svish

Posted 2014-12-02T13:46:12.017

Reputation: 27 731

Answers

20

Windows compresses the WinSxS folder by removing replaced updates ad compressing files which can't be uninstalled and which are not used into diffs. This takes a lot of CPU power and thats why you see a high CPU usage.

You can analyze the CPU usage with xperf/WPA to verify this.

magicandre1981

Posted 2014-12-02T13:46:12.017

Reputation: 86 560

Doesn't it also calculate the space saved when compressing the oldest files (those not opened recently)? – Justin Goldberg – 2014-12-02T21:43:38.550

Nice video! Will definitely check into that tool :D – Svish – 2014-12-03T09:18:53.123

@Svish have you verified this? Is the question answered? – magicandre1981 – 2015-12-23T05:23:56.190

4

I can confirm that Windows Updates Cleanup takes almost 100% CPU usage, it's all about WinSxS compression and reintegration of updates:

Windows Cleanup 100% CPU usage

I started cleanup first time after Windows install and it took 2 hours to finish 50%. From graph you can see, that at first lot of reading of small files from disk (second graph), and after 100% CPU usage for compression, and so on.

Arunas Bartisius

Posted 2014-12-02T13:46:12.017

Reputation: 895

2

Just to put fire on the question, i had done on an inmutable virtual machine the next steps:

  1. Boot a SystemRescueCD and mount the windows partition
  2. List all files on disk to a log file (less than a minute)
  3. Reboot onto Windows
  4. Let cleanmgr do its job and take note of the time (more than eight hours)
  5. Reboot onto SystemRescueCD and mount the windows partition
  6. List all files on disk to another log file (less than a minute)
  7. Reset back the inmutable virtual machine
  8. Reboot onto Windows
  9. Do a FOR /F ... to delete such files in an admin command prompt (cmd) and take note of the time (less than five minutes)

Please note i rebooted onto Linux SystemRescueCd just to be able to list all files, also the ones looked by Window and also to do such listing as fast as possible (no acl check, etc).

Also please note that on cleanmgr i only count only time after search ends (the search itself also tooks more than six hours).

So deleting the files is not the botle neck (the delete itself only need five minutes but cleanmgr takes more than eight hours).

The delete it self can go so fast since the host is a Linux, that speed is caused just because Linux caches all writes to the fixed size file that is used as the disk on the virtual machine (24GiB), and since i have 64GiB of RAM and i let to the Windows 16GiB it does not need to use SWAP neither PAGEFILE.SYS; also more, Linux can hold the full file on RAM (i tested with that file on RAM and delete times go done a lot, but cleanmgr time does not drop down).

Yes, if i put the virtual disk totally on RAM, cleanmgr time does not drop but if i manaully delete the files, that time drops down just a little (Linux write cache is great when you have 64GiB of RAM on it).

My configuration for the tests:

  • Host: Linux 64bits distro over a hardware that main parts are AMD Deca Core 5GHz with 64GiB of RAM and a Sata Raid0 controller with two WD VelociRaptor HDDs (able to sustain a combined secuential write speed of near 1GiB/s and a 4K random write of 300MiB/s); cost is >ten thousand euros
  • Guest: Windows 10 Home with 16GiB of ram and a Virtual disk of 24GiB

Test1: Having the 24GiB virtual disk on the physical harddisks Test2: Having the 24GiB virtual disk on the Linux host RAM

I am afraid that cleanmgr is doing something on the windows registry per each file it deletes (accessing the registry is very low).

Monitors i had:

  • CPU usage on the Linux host and on the Windows Guest (most time between 0% and 1%, with pikes of 5%, no more)
  • HDD usage on the Linux host (near 0% when cleanmgr was working, with pikes of 2%), Linux disk cache is really great avoiding dato be sent to real disk
  • HDD usage on the Windows guest (most time between 2% and 15% when cleanmgr was working, with pikes of 28%)

So clearly cleanmgr is doing a lot of undetectable work, maybe sleep times? Maybe not, if it is accesiing registry, the CPU for that is not counted by Windows monitors, HDD on that is not used (registry is on RAM), etc., and each access to the registry can take more than a second... try your own program to access registry by opening the registry and closing it per each action versus letting the HK* opened... it is a great difference.

So if cleanmgr does for each file:

  1. Open registry HK*
  2. Consult whatever needs and/or do some writes on registry
  3. Close registry HK*

It can take as much as two or three seconds per operation, and if it does one operation per file, it can take near one hour per each thousand of files... my count of files was just a little bit more than 40000 files, so 40000 files / 8 hours is processing one file each 1.3 seconds... on the other side, deleting them on a script (after knowing wich ones) tooks less than five minutes, that is deleting about 133 per second... a hufe difference.

So definitly cleanmgr time is not caused by the deletion it self! What else is it doing? Please M$ improve that speed!

Claudio

Posted 2014-12-02T13:46:12.017

Reputation: 21

1

This is not an answer to the original question. If you have a new question please ask your own question (referencing this one for context if it helps).

– DavidPostill – 2017-02-28T11:43:33.097

Why didn't you use the process monitor (https://docs.microsoft.com/en-us/sysinternals/downloads/procmon) to analyze the actions of windows registry etc?

– cybernard – 2018-04-11T01:22:38.693

im*mutable sp. – StingyJack – 2018-09-08T14:07:02.803

2

One way to mitigate this problem and speed up the "Windows Update Cleanup" phase of "Disk Cleanup" a bit is to temporarily set a higher priority for the TiWorker.exe process for the duration of the so-called 'cleanup' activity.

In the Task Manager 'Details' pane, find TiWorker.exe and right-click to show a pop-up menu. For 'Set Priority," choose "Above Normal." For me, this increased the TiWorker CPU activity--from averaging around 16%--up to its full potential of 25% (on a 4-core machine).

enter image description here

Aside from the miniscule speedup--and possibly good feeling of accomplishment--this provides, there's little that can be done since apparently "Windows Update Cleanup" is coded in a way that, for some unknown reason, proceeds file-by-file, making the overall work horribly disk-bound.

And also, do know that the process is most probably not hung, and will indeed complete someday. To verify this (and to yet further distract yourself during this annoying wait time), you can check the 'Disk Activity' section of Resource Monitor (run "resmon.exe").

Glenn Slayden

Posted 2014-12-02T13:46:12.017

Reputation: 803

Very nice speed up! – subjectivist – 2018-09-17T20:04:56.163

1

Deleting 20 files of 5mb in size will take more time to delete than 10 files of 64mb in size. This is because when system deletes files, it doesn't really delete them (as empty space on a drive can be made of "noise" bits) but just overwrites the front part that says, "A file starts here" with gibberish and then it will happily overwrite that and the following parts later when you want to use the space. In reality, deleting is the same as writing otherwise.

The thing with disk cleanup, is the things it cleans are usually LOTS of small files (internet cookies, temporary files, etc.). As such, it does a lot more writing to the disk than a lot of other things, and can take as much time as installing something new, due to the volume being written to disk.

liljoshu

Posted 2014-12-02T13:46:12.017

Reputation: 161

The original question specifically asks about Disk Cleanup. – liljoshu – 2015-12-26T21:59:28.620

0

Another word of warning: part of what takes up the time is that, when cleaning up WinSxS, Disk Clean-up has first to uncompress a lot of files. So disk usage actually increases for the first part of the clean-up!

JonP

Posted 2014-12-02T13:46:12.017

Reputation: 101

-2

Another way to speed up the process is to run the Disk Cleanup for about 5 min. Stop it, then re-start it, and it will whizz through in a jiffy! Also, De-Fragging or Optimizing your HDD/SSD beforehand will speed things up, just remember to de-frag/Optz it again after cleanup.

Bluesdigital

Posted 2014-12-02T13:46:12.017

Reputation: 1

Please don't defragment your SSD it wears out read and write cycles and has almost no gain. – Nordlys Jeger – 2018-11-06T10:57:49.520

@NordlysJeger Windows itself does defrag for SSDs by design

– magicandre1981 – 2018-11-06T15:48:18.163