none
Memory leaks in C# hosted Powershell RRS feed

  • Question

  • Hi,

    i have a simple app, that's creating a Powershell runspace:

                var iis = InitialSessionState.CreateDefault();
                iis.ExecutionPolicy = ExecutionPolicy.Unrestricted;
                iis.LanguageMode = PSLanguageMode.FullLanguage;
                iis.ThreadOptions = PSThreadOptions.UseNewThread;
                iis.DisableFormatUpdates = true;
    
                m_Settings = new PSInvocationSettings
                {
                    Host = new PSHostProxy(new PSUIStub()),
                };
    
                m_RunspacePool = RunspaceFactory.CreateRunspacePool(iis);
                m_RunspacePool.CleanupInterval = new TimeSpan(0, 0, 10);
                m_RunspacePool.SetMaxRunspaces(1);
                m_RunspacePool.SetMinRunspaces(1);
                m_RunspacePool.Open();


    And then uses it to execute several PowerShell scripts. Each RUN is invoked by external event (runs in another thread) and happens ussualy with 20-50 seconds after the foregoing RUN:

    RUN:
    {
      foreach(var scriptName in scripts)
      {
        var ps = PowerShell.Create();
        ps.RunspacePool = m_RunspacePool;
        var input = new PSDataCollection<string>();
    
        ps.AddCommand(Path.Combine(scriptPath, scriptName));
        ps.AddArgument(args);
        
        // Call
        ps.Invoke(input, m_Output, m_Settings);
        ...
        m_Output.Clear();
        input.Clear();
        input.Dispose();
    
        ps.Dispose();
        ps = null;
      }
    }

    And memory usage of this keeps raising during the run, although the scripts are always the same and even their results are the same. Each cycle creates a bunch (well like thousands of them, but i suppose the number is given by length of the executed scripts...) of SessionStateCmdLetEntry and those are never disposed and GCed, because of reference by a TimerCallback created by RunspacePoolInternal. This can take up to 1GB of memory per day...

    Does anyone have tips, what else should I dispose or clear to avoid stashing the SessionStateCmdLetEntries? Or any workarounds for this?

    I just need a long running process, that invokes several Powershell scripts and does not eat up memory.. I've also tried not reusing the RunSpacePool and creating a new Runspace for each RUN and even for each script, but it did not help at all...

    Thanks for any help...


    Thursday, March 14, 2019 11:05 AM

Answers

  • Shows out, that the memory leak is not caused by C# code, but by PowerShell script i've run through this code. The script creates its own RunspacePool and PowerShell instance and that caused the leak. So it is not a C# problem anymore.
    • Marked as answer by Karel Pokorny Friday, March 22, 2019 10:35 AM
    Friday, March 22, 2019 10:35 AM

All replies

  • Hi

    Thank you for posting here.

    According to your description, you want to deal with the problem that Memory leaks in C# hosted Powershell.

    Based on my research, the following link could help you.

    https://stackoverflow.com/questions/35640564/c-sharp-system-management-automation-powershell-class-leaking-memory-on-invoke

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 6:28 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 19, 2019 3:25 AM
    Moderator
  • Shows out, that the memory leak is not caused by C# code, but by PowerShell script i've run through this code. The script creates its own RunspacePool and PowerShell instance and that caused the leak. So it is not a C# problem anymore.
    • Marked as answer by Karel Pokorny Friday, March 22, 2019 10:35 AM
    Friday, March 22, 2019 10:35 AM