core icon indicating copy to clipboard operation
core copied to clipboard

dotnet core v3.1/3.0 Process.Start execute shell command Memory Leak

Open lychee-ui opened this issue 6 years ago • 18 comments

dotnet core v3.1/3.0 Process.Start execute shell command Memory Leak

System: Ubuntu 18.04 / CentOS 7.6 / SUSE 15 SDK: 3.1.100

ex:

var psi = new ProcessStartInfo("dotnet");
psi.RedirectStandardOutput = true;

psi.Arguments = $"--version";
using (var proc = Process.Start(psi))
{
    var output = proc.StandardOutput.ReadToEnd();
    var version = output.Trim();

    if (!proc.HasExited)
    {
        proc.Kill();
    }
}

lychee-ui avatar Dec 13 '19 01:12 lychee-ui

I have find the same issue.

irac-ding avatar May 06 '20 03:05 irac-ding

any response ??

irac-ding avatar May 06 '20 04:05 irac-ding

Please Pay Attention !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! System: CentOS 7.2 SDK: 3.1.201

ex: class Program { static void Main(string[] args) { Console.WriteLine("Press any key to continue"); Console.ReadLine(); while (1 == 1) { List stdout = RunTaskAndReturnStdout("dotnet", $"--version"); } } }

public static List RunTaskAndReturnStdout(string fileName, string arguments, string workingDirectory = "") { List ret = new List(); try { using (Process proc = new Process()) { proc.StartInfo.FileName = fileName; proc.StartInfo.Arguments = arguments; proc.StartInfo.UseShellExecute = false; if (!string.IsNullOrWhiteSpace(workingDirectory)) proc.StartInfo.WorkingDirectory = workingDirectory; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); while (!proc.StandardOutput.EndOfStream) { string line = proc.StandardOutput.ReadLine(); // Console.WriteLine(logstr); if (!string.IsNullOrWhiteSpace(line)) ret.Add(line); } return ret; } } catch (Exception ex) { return ret; } }

irac-ding avatar May 06 '20 07:05 irac-ding

We have the exact same problem. Is there any progress on this or at least a known workaround?

BagJo avatar Jun 18 '20 19:06 BagJo

I'm currently investigating an out of memory problem on our linux device (arm32) and came accross this issue. Every minute we start a process to collect dhcp information which over time seems to allocate/consume more and more memory in the process which does the Process.Start

I did a test run using the modified sample code from @lychee-ui in which i start the cli tool free

var psi = new ProcessStartInfo("free");
psi.RedirectStandardOutput = true;
using (var proc = Process.Start(psi))
{
    var output = proc.StandardOutput.ReadToEnd();
    var version = output.Trim();
    proc.WaitForExit();
    if (!proc.HasExited)
    {
        proc.Kill();
    }
}

Every 100th call i logged the memory consumption of the running process using process.WorkingSet64 from Process.GetCurrentProcess()

After 69500 calls of Process.Start(psi) the memory consumtion has grown by 70% compared to the working set at start of the proccess. Calling it that often in that short time may not be common but if the memory is never released this will cause OOM over time which happens on our device after about 1 month.

mdisg avatar Jul 06 '20 13:07 mdisg

FYI: Mono project had a similar issue. The bug has already been fixed there. Maybe that can help to investigate the issue further.

BagJo avatar Jul 14 '20 16:07 BagJo

@BagJo i looked into the code of Process.Start and have not found the retrieval of the path environment variable maybe i missed it. As we use .NET 3.1 i only looked into that sources which are:

pal_process.c Interop.ForkAndExecProcess.cs

mdisg avatar Aug 06 '20 13:08 mdisg

Can confirm the Memory Leak. System Ubuntu 18.04. SDK 3.1.401

Anybody knows a fix or workaround ?

ThomasBoettcherHolschen avatar Aug 20 '20 17:08 ThomasBoettcherHolschen

@ThomasBoettcherHolschen my workaround for the moment is a self implemented interop assembly which starts the process i need frequently by execv. I do not pass the arguments from managed code which the .net code does. But that's not an ideal solution.

mdisg avatar Aug 21 '20 05:08 mdisg

It's disappointing that Microsoft has left such a serious bug alone for a year

irac-ding avatar Aug 21 '20 05:08 irac-ding

A leak in Process.Start was fixed in .NET Core 3.1.7: https://github.com/dotnet/corefx/pull/42941, https://github.com/dotnet/runtime/issues/36661.

tmds avatar Oct 27 '20 11:10 tmds

I verified the fix with a quick test using .NET Core 3.1.9 using the sample code. The memory leak is gone.

mdisg avatar Oct 27 '20 13:10 mdisg

Thanks a lot! Great news!

ThomasBoettcherHolschen avatar Oct 27 '20 13:10 ThomasBoettcherHolschen

I verified the fix with a quick test using .NET Core 3.1.9 using the sample code. The memory leak is gone.

How to install 3.1.9 in CentOS 7 ? Anyone any Suggest

irac-ding avatar Oct 28 '20 02:10 irac-ding

@irac-ding you download the latest version from here https://dotnet.microsoft.com/download/dotnet-core/3.1

mdisg avatar Nov 02 '20 06:11 mdisg

This should be closed as resolved in both .net core 3.1.7 and .net core 5, no?

freddyrios avatar Feb 17 '21 08:02 freddyrios

@stephentoub @danmoseley can you let us know if this issue was already resolved? Trying to close some old issues.

mairaw avatar May 24 '23 19:05 mairaw

I believe so.

stephentoub avatar May 24 '23 20:05 stephentoub