PowerShell单行占用大量内存

rwqw0loc  于 2022-12-18  发布在  Shell
关注(0)|答案(2)|浏览(232)

我正在尝试使用下面的命令。每当我使用这个命令时,内存使用率都非常高。有没有办法在完成相同任务的同时降低内存使用率?

Get-ChildItem -Path "C:\" -Recurse |
  sort -descending -property length | 
  select -first 20 fullname, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}}
vvppvyoh

vvppvyoh1#

与你现在所做的相比,这应该是非常有效的,基本上,它利用SortedSet<T> Class和一个实现IComparableIEquatable<T>的定制PowerShell Class。匿名函数内部的逻辑确保排序集始终具有最多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()
    }
}
mrfwxfqh

mrfwxfqh2#

这里有一个主要是cmd的解决方案,运行时间减半(4分钟对我来说是8分钟),而且没有大量的ram。

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

相关问题