我试图使用下面的命令。无论何时我使用它,内存使用量都非常高。是否有一种方法可以在完成相同任务的同时降低内存使用量?
Get-ChildItem -Path "C:" -Recurse |
sort -descending -property length |
select -first 20 fullname, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}}
与你目前所做的相比,这应该是非常有效的,基本上它利用了SortedSet<T>
类和实现IComparable
和IEquatable<T>
的自定义PowerShell类。匿名函数内部的逻辑确保了排序集的元素数始终不超过20个(根据需要更改$sorted.Count -lt 20
,这取决于您想要的结果有多少文件),因此内存使用应该低得多。
class SimpleFile : System.IComparable, System.IEquatable[object] {
[string] $FullName
[Int64] $Length
[double] $Gigabytes
SimpleFile([IO.FileInfo] $File) {
$this.FullName = $File.FullName
$this.Length = $File.Length
$this.Gigabytes = [Math]::Round($File.Length / 1Gb, 2)
}
[int] GetHashCode() {
return $this.FullName.GetHashCode()
}
[int] CompareTo([object] $That) {
if($diff = $this.Length.CompareTo($That.Length)) {
return $diff
}
return 0
}
[bool] Equals([object] $That) {
return $this.FullName -eq $That.FullName
}
}
$result = Get-ChildItem C: -Recurse -File -EA 0 | & {
begin {
$sorted = [System.Collections.Generic.SortedSet[SimpleFile]]::new()
}
process {
if($sorted.Count -lt 20) {
$null = $sorted.Add($_)
return
}
if($sorted.Min.Length -lt $_.Length) {
$null = $sorted.Remove($sorted.Min)
$null = $sorted.Add($_)
}
}
end {
$sorted.Reverse()
}
}
这是一个主要的cmd解决方案,运行时间缩短了一半(4分钟对我来说是8分钟),而且没有太多的内存可言。
cmd /c 'dir /s c: | findstr /v "Directory of" | findstr /v "<DIR>" | findstr /v "<JUNCTION>" | findstr /v "Total Files Listed:" | sort.exe /+20' | select -last 20
09/13/2018 04:45 PM 362,337,255 78ec5a6bf483ef155dc2a311e526f8a5.cab
09/23/2022 02:24 PM 379,584,512 clonezilla-live-3.0.1-8-amd64.iso
09/13/2018 04:45 PM 445,038,363 8bb1cf01f3ce1952f356d0aff91dbb2f.cab
09/13/2018 04:45 PM 445,038,363 8bb1cf01f3ce1952f356d0aff91dbb2f.cab
04/13/2012 03:02 PM 481,143,404 Data1.cab
02/23/2022 12:10 PM 540,587,242 Miller_MUS171_Lecture02_v2.mp4
05/17/2017 06:27 PM 551,624,704 maple.help
09/13/2018 04:44 PM 600,753,345 a32918368eba6a062aaaaf73e3618131.cab
09/13/2018 04:44 PM 600,753,345 a32918368eba6a062aaaaf73e3618131.cab
11/10/2022 03:12 AM 641,728,512 BaseLayer.vhdx
11/10/2022 03:12 AM 641,728,512 BaseLayer.vhdx
11/09/2022 09:17 AM 687,780,220 Windows10.0-KB5019959-x64.cab
12/12/2017 01:45 PM 740,060,956 2ddf168b.msi
09/13/2018 04:45 PM 760,142,380 9722214af0ab8aa9dffb6cfdafd937b7.cab
09/13/2018 04:45 PM 760,142,380 9722214af0ab8aa9dffb6cfdafd937b7.cab
06/02/2016 03:19 PM 894,730,632 392b35b.msi
04/18/2021 03:03 PM 905,672,704 37b241d4.msi
06/13/2022 12:52 PM 905,672,704 37b241d7.msi
04/18/2021 03:03 PM 905,672,704 Stata17.msi
01/12/2018 03:06 PM 1,075,667,093 windows10.0-kb4056891-x64.msu