I'm having a bit of trouble understanding exactly what is going and why it is happening. I have a small function grabbing all the bytes of a large 133MB PNG image file, storing it in a byte array, and returning it. There is either some behavior I don't understand, or perhaps a bug in PowerShell?
$TestFile = 'C:\test.png'
function GetByteArray($InputFile) {
$ByteArray = [IO.File]::ReadAllBytes($InputFile)
Write-Host ( 'Type: ' + $ByteArray.GetType() )
Write-Host ( 'Size: ' + $ByteArray.Length )
return $ByteArray
}
$NewArray = GetByteArray -InputFile $TestFile
Write-Host ( 'Type: ' + $NewArray.GetType() )
Write-Host ( 'Size: ' + $NewArray.Length )
pause
I am expecting the function to return a [Byte[]] about 133MB in size, but it does not. Instead PowerShell consumes about 5GB RAM, prints out the error message below, and returns a [System.Object[]].
Type: byte[]
Size: 140151164
Array dimensions exceeded supported range.
At F:\test.ps1:10 char:10
+ return $ByteArray
+ ~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], OutOfMemoryException
+ FullyQualifiedErrorId : System.OutOfMemoryException
Type: System.Object[]
Size: 134217728
Press Enter to continue...:
What am I not understanding? Why is the byte array being converted to an object? Why is eating almost all my RAM?