dotnet core v3.1/3.0 Process.Start execute shell command Memory Leak
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();
}
}
I have find the same issue.
any response ??
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; } }
We have the exact same problem. Is there any progress on this or at least a known workaround?
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.
FYI: Mono project had a similar issue. The bug has already been fixed there. Maybe that can help to investigate the issue further.
@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:
Can confirm the Memory Leak. System Ubuntu 18.04. SDK 3.1.401
Anybody knows a fix or workaround ?
@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.
It's disappointing that Microsoft has left such a serious bug alone for a year
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.
I verified the fix with a quick test using .NET Core 3.1.9 using the sample code. The memory leak is gone.
Thanks a lot! Great news!
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 you download the latest version from here https://dotnet.microsoft.com/download/dotnet-core/3.1
This should be closed as resolved in both .net core 3.1.7 and .net core 5, no?
@stephentoub @danmoseley can you let us know if this issue was already resolved? Trying to close some old issues.
I believe so.