Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lychee-ui opened this issue Dec 13, 2019 · 18 comments
Closed

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

lychee-ui opened this issue Dec 13, 2019 · 18 comments

Comments

@lychee-ui
Copy link

lychee-ui commented Dec 13, 2019

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();
    }
}
@irac-ding
Copy link

I have find the same issue.

@irac-ding
Copy link

any response ??

@irac-ding
Copy link

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;
}
}

@BagJo
Copy link

BagJo commented Jun 18, 2020

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

@mdisg
Copy link

mdisg commented Jul 6, 2020

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.

@BagJo
Copy link

BagJo commented Jul 14, 2020

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

@mdisg
Copy link

mdisg commented Aug 6, 2020

@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

@ThomasBoettcherHolschen
Copy link

ThomasBoettcherHolschen commented Aug 20, 2020

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

Anybody knows a fix or workaround ?

@mdisg
Copy link

mdisg commented Aug 21, 2020

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

@irac-ding
Copy link

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

@tmds
Copy link
Member

tmds commented Oct 27, 2020

A leak in Process.Start was fixed in .NET Core 3.1.7: dotnet/corefx#42941, dotnet/runtime#36661.

@mdisg
Copy link

mdisg commented Oct 27, 2020

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

@ThomasBoettcherHolschen

Thanks a lot! Great news!

@irac-ding
Copy link

irac-ding commented Oct 28, 2020

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

@mdisg
Copy link

mdisg commented Nov 2, 2020

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

@freddyrios
Copy link

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

@mairaw
Copy link
Contributor

mairaw commented May 24, 2023

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

@stephentoub
Copy link
Member

I believe so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants