I'm trying to download a 10MB file and store it as an array for further processing.
Everything seems fine when using a direct call to (New-Object System.Net.WebClient).DownloadData("<url>"). But if I wrap it inside a function and return the result of the call to WebClient::DownloadData memory footprint increases to around 500mb.
The function that I use:
function My-Download {
param (
[Parameter(Mandatory = $True, Position = 1)] [String] $UrlCode
)
(New-Object System.Net.WebClient).DownloadData($UrlCode)
}
$x = My-Download("https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1280_10MG.mp4")
The reason I wrapped it inside of the function is that I also do additional processing on the data before returning it but even this small example illustrates the problem.
Calling $x = (New-Object System.Net.WebClient).DownloadData("https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_1280_10MG.mp4") results in 83MB:

Calling the above function results in 500MB:

What is the reason for such a high memory usage and what can I do to optimize it?
Powershell version:
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 407
$x = My-Download "https://...". Also, the first index for parameter position is 0, not 1.(New-Object System.Net.WebClient).DownloadData($UrlCode)->,(New-Object System.Net.WebClient).DownloadData($UrlCode)