【问题标题】:Problem if I run a process for second time, increase in memory usage c#问题如果我第二次运行一个进程,内存使用量会增加c#
【发布时间】:2019-12-20 20:17:55
【问题描述】:

我的问题是第一个进程最多在一秒钟内执行,将消耗大约 20MB 并且会恢复正常,但是每次我在此之后重新启动进程,即使一个小时后也需要大约 1 分钟做这项工作,内存会超过 1,200MB,之后不会下降。 唯一的解决方法是在每次使用后重新启动应用程序。

using (var compiler = new Process
                        {
                            StartInfo =
                            {
                                FileName = _installPath + @"\Papyrus Compiler\PapyrusCompiler.exe",
                                Arguments = $@"{(string) tab.Tag} -f=""TESV_Papyrus_Flags.flg"" -i=""{_installPath}\Data\scripts\source"" -o=""{_installPath}\Data\scripts""",
                                UseShellExecute = false,
                                RedirectStandardOutput = true,
                                RedirectStandardError = true,
                                CreateNoWindow = true
                            }
                        })
                        {
                            compiler.Start();

                            Console.WriteLine(compiler.StandardError.ReadToEnd());
                            Console.WriteLine(compiler.StandardOutput.ReadToEnd());

                            compiler.WaitForExit();
                        }

提前感谢您的帮助。

编辑: 做了一些改变,但根本没有帮助

using (var compiler = new Process
                    {
                        StartInfo =
                        {
                            FileName = _installPath + @"\Papyrus Compiler\PapyrusCompiler.exe",
                            Arguments = $@"{tab.Tag as string} -f=""TESV_Papyrus_Flags.flg"" -i=""{_installPath}\Data\scripts\source"" -o=""{_installPath}\Data\scripts""",
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                            CreateNoWindow = true
                        }
                    })
                    {
                        string eOut = null;
                        compiler.ErrorDataReceived += (sender, e) =>
                        {
                            eOut += e.Data;
                        };

                        compiler.Start();

                        compiler.BeginErrorReadLine();
                        var ou = compiler.StandardOutput.ReadToEnd();

                        compiler.WaitForExit();

                        Console.WriteLine(eOut);
                        Console.WriteLine(ou);
                    }

还有我第一次将控制台重定向到文本框时没有提到的事情

        readonly TextWriter _writer;

    public Form1(string args)
    {
        InitializeComponent();

        // Instantiate the writer
        _writer = new TextBoxStreamWriter(output);
        // Redirect the out Console stream
        Console.SetOut(_writer);
    }

【问题讨论】:

    标签: c# memory process


    【解决方案1】:

    经过这么长时间和拉扯我的头发后,我决定尝试其他方法来解决问题。 我创建了一个仅使用此方法的新应用程序,单击按钮以测试它是否会在第二次尝试编译脚本时挂起。令我惊讶的是问题消失了。

    然后我决定尝试通过在原始应用程序中添加更多操作来重现我的错误。一旦我添加了 output.Clear();开始挂了,发现问题

    我也尝试过 output.Text = "";它仍然会开始挂起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 2021-02-05
      • 2016-06-14
      相关资源
      最近更新 更多