Skip to content

Invoke-RestMethod is slow and uses a lot of memory #6199

Closed

Description

@KarlGW

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

  1. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    can you try your code without the extra stuff to eliminate other potential .NET Framework vs .NET Core differences?

    something simpler like this:

    Measure-Command  {
        $data = do {
            $call = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers -Verbose:$false
            $uri = $call.nextLink
            $call.data
        } while ($uri) 
    }
  2. KarlGW commented on Feb 20, 2018

    @KarlGW
    Author

    I ran your snippet (with the correct URIs of course):

    Days              : 0
    Hours             : 0
    Minutes           : 12
    Seconds           : 9
    Milliseconds      : 674
    Ticks             : 7296743233
    TotalDays         : 0,00844530466782407
    TotalHours        : 0,202687312027778
    TotalMinutes      : 12,1612387216667
    TotalSeconds      : 729,6743233
    TotalMilliseconds : 729674,3233
    

    In 6.0.1.

    And for the case of comparison (5.1):

    Days              : 0
    Hours             : 0
    Minutes           : 3
    Seconds           : 16
    Milliseconds      : 401
    Ticks             : 1964017691
    TotalDays         : 0,00227316862384259
    TotalHours        : 0,0545560469722222
    TotalMinutes      : 3,27336281833333
    TotalSeconds      : 196,4017691
    TotalMilliseconds : 196401,7691
    

    Also, 6.0.1 does not release the memory until I kill the process, or do a [System.GC]::Collect().

  3. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    Is the URI localhost by any chance (something like http://localhost:8762/api/someendpoint)? If so, there is a known issue with .NET Core and resolving localhost. Try switching it to 127.0.0.1 or ::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.

  4. KarlGW commented on Feb 20, 2018

    @KarlGW
    Author

    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.

  5. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    .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?

  6. KarlGW commented on Feb 20, 2018

    @KarlGW
    Author

    It returns it's data in JSON. In the form of

    {
        id: "id",
        data: {
            <more JSON with a lot of properties>
        },
        nextLink: <uri for next chunk of data>
    }
    
  7. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    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.

  8. iSazonov commented on Feb 20, 2018

    @iSazonov
    Collaborator

    We need live repo. Is the URI public?

  9. KarlGW commented on Feb 20, 2018

    @KarlGW
    Author

    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:

    {
        "id": "string",
        "data": [
            {                       
            "accountId": 0,
            "productId": 0,
            "resourceLocationId": 0,
            "consumedServiceId": 0,
            "departmentId": 0,
            "accountOwnerEmail": "string",
            "accountName": "string",
            "serviceAdministratorId": "string",
            "subscriptionId": 0,
            "subscriptionGuid": "string",
            "subscriptionName": "string",
            "date": "2017-04-27T23:01:43.799Z",
            "product": "string",
            "meterId": "string",
            "meterCategory": "string",
            "meterSubCategory": "string",
            "meterRegion": "string",
            "meterName": "string",
            "consumedQuantity": 0,
            "resourceRate": 0,
            "Cost": 0,
            "resourceLocation": "string",
            "consumedService": "string",
            "instanceId": "string",
            "serviceInfo1": "string",
            "serviceInfo2": "string",
            "additionalInfo": "string",
            "tags": "string",
            "storeServiceIdentifier": "string",
            "departmentName": "string",
            "costCenter": "string",
            "unitOfMeasure": "string",
            "resourceGroup": "string"
            }
        ],
        "nextLink": "string"
    }
    
  10. KarlGW commented on Feb 20, 2018

    @KarlGW
    Author

    Ilya (@iSazonov) unfortunately the API is not public, you need an enrollmentnumber and an API key.

  11. KarlGW commented on Feb 22, 2018

    @KarlGW
    Author

    I did a test with Invoke-WebRequest and yes, you are correct, it's the part that converts the data to JSON that is significantly slower.

  12. markekraus commented on Feb 22, 2018

    @markekraus
    Contributor

    Karl (@KarlGW) In your test, did it include ConvertFrom-Json?

  13. KarlGW commented on Feb 22, 2018

    @KarlGW
    Author

    Mark Kraus (@markekraus) Yes, in the tests after the mentioned one (first I only pulled the results), I put in a ConvertFrom-Json and time increased by a significant bit.

  14. markekraus commented on Feb 22, 2018

    @markekraus
    Contributor

    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.

  15. 11 remaining items

  16. RealDrGordonFreeman commented on Mar 5, 2019

    @RealDrGordonFreeman

    there are many complaints about it's performance in multiple forums and sites

    Dr. Gordon Freeman (@RealDrGordonFreeman) could you please add references to these scenarios and use cases?

    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.)

  17. iSazonov commented on Mar 5, 2019

    @iSazonov
    Collaborator

    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.

  18. john8329 commented on Aug 29, 2019

    @john8329

    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:

    Invoke-WebRequest `
                    -Uri "$url/$name" `
                    -Credential $cred `
                    -Method Put `
                    -ContentType 'application/octet-stream' `
                    -InFile $File

    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.

  19. vexx32 commented on Aug 29, 2019

    @vexx32
    Collaborator

    @j0hnwhyte does that reproduce in the latest v7 preview releases?

  20. iSazonov commented on Aug 29, 2019

    @iSazonov
    Collaborator

    @j0hnwhyte Please open new issue with repo steps.

  21. john8329 commented on Aug 30, 2019

    @john8329

    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

  22. added
    Resolution-External
    on Oct 15, 2019
  23. david-tap commented on Nov 18, 2019

    @david-tap

    #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.

  24. 9whirls commented on May 28, 2020

    @9whirls

    Reproduced this issue on 7.0.1 on Mac.

    measure-command {Invoke-webrequest -Uri 'https://disease.sh/v2/states' }                                                                       
                                                                                                                                                                                                          Days              : 0                                                                                                                                                                                 Hours             : 0                                                                                                                                                                                 Minutes           : 0                                                                                                                                                                                 
    Seconds           : 1
    Milliseconds      : 30
    Ticks             : 10306922
    TotalDays         : 1.19293078703704E-05
    TotalHours        : 0.000286303388888889
    TotalMinutes      : 0.0171782033333333
    TotalSeconds      : 1.0306922
    TotalMilliseconds : 1030.6922
    
    
    measure-command {Invoke-restmethod -Uri 'https://disease.sh/v2/states' }
    
    Days              : 0
    Hours             : 0
    Minutes           : 1
    Seconds           : 16
    Milliseconds      : 175
    Ticks             : 761751440
    TotalDays         : 0.000881656759259259
    TotalHours        : 0.0211597622222222
    TotalMinutes      : 1.26958573333333
    TotalSeconds      : 76.175144
    TotalMilliseconds : 76175.144
    
    get-host
    
    Name             : ConsoleHost
    Version          : 7.0.1
    InstanceId       : 47287e23-00c2-4ba5-a503-cbaa155f27cc
    UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture   : en-US
    CurrentUICulture : en-US
    PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
    DebuggerEnabled  : True
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace
    
  25. iSazonov commented on May 28, 2020

    @iSazonov
    Collaborator

    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.

  26. PMaackFT commented on Jul 2, 2025

    @PMaackFT

    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" -UseBasicParsing
    The 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-ExternalWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions