Home > Software design >  PowerShell Single Line Huge Memory Usage
PowerShell Single Line Huge Memory Usage

Time:12-03

I'm trying to use the below command. Whenever I use this, memory usage is extremely high. Is there a way to lower memory usage while completing the same task?

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

CodePudding user response:

This should very efficient compared to what you're currently doing, basically it leverages the SortedSet<T> Class and a custom PowerShell Class implementing IComparable and IEquatable<T>. The logic inside the anonymous function ensures that the sorted set will always have a maximum of 20 elements (change the $sorted.Count -lt 20 as needed depending on how many files you want as result) so memory usage should be much lower.

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()
    }
}

CodePudding user response:

Here's a mostly cmd solution that runs in half the time (4 min vs 8 min for me), and no great amount of ram to speak of.

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
  • Related