Description
Steps to reproduce
Quick explanation, when describing it as slow and uses a lot of memory, I'm comparing Invoke-RestMethod from Windows PowerShell 5.1 with Invoke-RestMethod in PowerShell 6.0.1.
I'm running the below snippt inside a function in my module:
while ($continue) {
$completed = $false
$retries = 0
# To retry until limit is reached.
while(-not $completed) {
try {
Write-Verbose "Calling $uri"
$call = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -Verbose:$false
$data += $call.data
$completed = $true
} catch {
if ($retries -ge $RetryCount) {
Write-Verbose "API call failed after $retries retries."
$completed = $true
} else {
Write-Verbose "API call failed. Retrying..."
Start-Sleep -Milliseconds $RetryDelay
$retries++
}
}
}
if ([string]::IsNullOrEmpty($call.nextLink)) {
$continue = $false
} else {
$uri = $call.nextLink
}
}
Expected behavior
Expected behaviour is that the same code snippet should take about the same time and memory usage when running.
Actual behavior
When running on Windows PowerShell the snippet takes about 4 minutes, and take about 1,5 GB RAM (some objects that are stored in memory to be transformed by another function).
With PowerShell 6.0.1 (both tried within a Docker Container, and with the pwsh executable locally). The snippet takes between 12-20 minutes, and RAM goes up to 4-5 GB.
Environment data
Name Value
---- -----
PSVersion 6.0.1
PSEdition Core
GitCommitId v6.0.1
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
If any more information is needed, I can hopefully provide this.
Activity
markekraus commentedon Feb 20, 2018
can you try your code without the extra stuff to eliminate other potential .NET Framework vs .NET Core differences?
something simpler like this:
KarlGW commentedon Feb 20, 2018
I ran your snippet (with the correct URIs of course):
In 6.0.1.
And for the case of comparison (5.1):
Also, 6.0.1 does not release the memory until I kill the process, or do a
[System.GC]::Collect().markekraus commentedon Feb 20, 2018
Is the URI
localhostby any chance (something likehttp://localhost:8762/api/someendpoint)? If so, there is a known issue with .NET Core and resolving localhost. Try switching it to127.0.0.1or::1. We ran into this in the tests inthe project and just switching to the IP brought the test runtimes down significantly.On the memory usage, you can't really compare .NET Framework and .NET Core. The garbage collection works differently. Unless the memory is an issue (resource exhaustion), I wouldn't be concerned about the difference in ram usage between 5.1 and 6.0.1.
KarlGW commentedon Feb 20, 2018
The URI is an external source, so not localhost.
The memory becomes somewhat of an issue if I'm running it inside a container in Docker, since it will kill the process. I can always increase the memory size of the container to mitigate that though.
markekraus commentedon Feb 20, 2018
.NET manages the memory for you so it should start garbage collection when it exhausts.
What kind of data is being returned by the API? JSON, XML, plaint-text, binary?
KarlGW commentedon Feb 20, 2018
It returns it's data in JSON. In the form of
markekraus commentedon Feb 20, 2018
Is it possible to share some details about the API? Perhaps some documentation and/or mocked/scrubbed JSON result? Everything I have tested shows 6.0.1 out-performing 5.1 in both time and memory consumption. So, I'm wondering if the slow down is in the JSON deserialization. That would be heavily dependent on the object shape but one change between 5.1 and 6.0.1 was the JSON engine.
iSazonov commentedon Feb 20, 2018
We need live repo. Is the URI public?
KarlGW commentedon Feb 20, 2018
Yes, it's not our own API, but one provided by Microsoft.
Here is a link to the documentation: https://docs.microsoft.com/en-us/rest/api/billing/enterprise/billing-enterprise-api-usage-detail
The JSON return is a bit down in the article. But I'll provide a mockup response (provided by the article) example below:
KarlGW commentedon Feb 20, 2018
Ilya (@iSazonov) unfortunately the API is not public, you need an enrollmentnumber and an API key.
KarlGW commentedon Feb 22, 2018
I did a test with
Invoke-WebRequestand yes, you are correct, it's the part that converts the data to JSON that is significantly slower.markekraus commentedon Feb 22, 2018
Karl (@KarlGW) In your test, did it include
ConvertFrom-Json?KarlGW commentedon Feb 22, 2018
Mark Kraus (@markekraus) Yes, in the tests after the mentioned one (first I only pulled the results), I put in a
ConvertFrom-Jsonand time increased by a significant bit.markekraus commentedon Feb 22, 2018
OK. Do you have an estimate of the number of objects per page that API is returning? since it looks like the slowdown is in the JSON processing, We may be able to reproduce problem with the mockup.
11 remaining items
RealDrGordonFreeman commentedon Mar 5, 2019
Sure.
https://stackoverflow.com/questions/28682642/powershell-why-is-using-invoke-webrequest-much-slower-than-a-browser-download
https://stackoverflow.com/questions/48268279/powershell-invoke-webrequest-is-consistently-very-slow-how-can-i-fix-this
https://stackoverflow.com/questions/14202054/why-is-this-powershell-code-invoke-webrequest-getelementsbytagname-so-incred
#5284
#2238
And many more. This cmdlet has problems. I think it needs to be reviewed.
(And also my own recent experiences with it. I had to write my own download function as a workaround.)
iSazonov commentedon Mar 5, 2019
Dr. Gordon Freeman (@RealDrGordonFreeman) Thanks for links. Issue with ProgressBar was fixed. Please try latest PowerShell Core version and report problems you find with it.
john8329 commentedon Aug 29, 2019
I have to add my own issue with this, unfortunately uploading a sequence of large files using this function results in the process using gigabytes of RAM and then showing clueless errors. This is the configuration I use:
The issue is reproducible systematically, and it's just a simple webdav server on the endpoint.
I know we're talking about Invoke-RestMethod, but it seems that they share the same code underneath.
vexx32 commentedon Aug 29, 2019
@j0hnwhyte does that reproduce in the latest v7 preview releases?
iSazonov commentedon Aug 29, 2019
@j0hnwhyte Please open new issue with repo steps.
john8329 commentedon Aug 30, 2019
I can confirm that the v7 preview does not exhibit the issue, therefore the thing seems to be solved. I'll also try v6 and see if that works as well. Thanks
david-tap commentedon Nov 18, 2019
#7698 was closed as a duplicate for this issue, but converting a JSON file of 287MB is still taking 8GB of RAM on the latest preview.
9whirls commentedon May 28, 2020
Reproduced this issue on 7.0.1 on Mac.
iSazonov commentedon May 28, 2020
Jian Liu (@9whirls) Please check on latest PowerShell 7.1 Preview. If you will see the problem please open new issue. Also please check on other platforms if you can.
PMaackFT commentedon Jul 2, 2025
For anyone still interested in this issue - I just downloaded a ~2.2 GB file from a website and only saw the RAM usage wiggle around in the ~300MB range using the following:
$WebDownloadURI = 'https://Website.com/FileName'$ProgressPreference = 'SilentlyContinue'Invoke-WebRequest -Uri $WebDownloadURI -OutFile "$WorkingDir\FileName.zip" -UseBasicParsingThe secret sauce is in the progress preference. Without it you're going to have a bad time. I don't know why. Feel free to speculate. This is the way. If you try to do the same with Invoke-RestMethod you will get the dreaded "Invoke-RestMethod : Stream was too long." error. YMMV