Skip to content

System.OutOfMemoryException thrown by Out-String in PS 7.4 #20904

@slavizh

Description

@slavizh

Prerequisites

Steps to reproduce

I am experiencing this strange issue with Out-String in PS 7.4 giving System.OutOfMemoryException in certain situations. I think it is the size of the object I am inputting for the cmdlet. I will try to provide steps for reproduction but it is hard as I can reproduce this on Azure Pipelines Hosted agents which run on containers. The issue appeared when we upgraded PS on the hosted agents from 7.2 to 7.4. Same code with same input was working fine before that. The code we run is the following:

$whatIfResult = Get-AzDeploymentWhatIfResult @deploymentParameters
($whatIfResult | Out-String).replace("$e[38;5;208m", "$($PSStyle.Foreground.Red)").replace("$e[38;5;77m" , "$($PSStyle.Foreground.Green)").Replace("$e[38;5;141m", "$($PSStyle.Foreground.Magenta)").Replace("$e[38;5;246m", "$($PSStyle.Foreground.BrightBlack)")

I have removed the replace part and still getting error: Error: Exception of type 'System.OutOfMemoryException' was thrown.
The error is not thrown every time if the results in variable whatIfResult are not big everything works. But as soon as the variable has some big results the error appears. If needed I can share more details but it needs to be in private as the parameters for cmdlet Get-AzDeploymentWhatIfResult are something I cannot share in public. I can also reproduce it with a file that I have downloaded from the pipeline logs that is in size 1.57 GBs. When I reproduce it I run it on my laptop with may more memory and it is on Windows but also PS 7.4. If I do Get-Content <file name> | Out-String I get the same error I am hoping that this was working fine in PS 7.2 can help you find what in the code of PS 7.4 for Out-String is causing it. I am not sure if it somehow depended on the Memory where PS 7.4 runs but as I have mentioned same machines with same capacity did not had the problem with 7.2.

Expected behavior

Out-String to succeed.

Actual behavior

Error: Exception of type 'System.OutOfMemoryException' was thrown.

Error details

CategoryInfo          : NotSpecified: (:) [Out-String], OutOfMemoryException
FullyQualifiedErrorId : System.OutOfMemoryException,Microsoft.PowerShell.Commands.OutStringCommand
InvocationInfo        :
    MyCommand        : Out-String
    ScriptLineNumber : 1
    OffsetInLine     : 91
    HistoryId        : 13
    Line             : Get-Content "C:\Users\stanislav.zhelyazkov\Downloads\logs_200053\1_Deploy solution.txt" | out-st
ring
    Statement        : out-string
    PositionMessage  : At line:1 char:91
                       + … .zhelyazkov\Downloads\logs_200053\1_Deploy solution.txt" | out-string
                       +                                                              ~~~~~~~~~~
    InvocationName   : out-string
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Ubuntu 20.04.6 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions