【问题标题】:Streaming/Tailing data over SSH using C# with SSH.NET leaks memory使用 C# 和 SSH.NET 通过 SSH 流式传输/拖尾数据会泄漏内存
【发布时间】:2015-06-01 16:23:52
【问题描述】:

我正在尝试使用 C# 通过 SSH 跟踪文件。该文件从一开始就被读取,然后在保持 SSH 连接的同时继续被监视数小时。我正在使用 SSH.NET 库为 SSH 提供功能。文件大小最高可达 ~2GB。当前的实现正在运行,但内存使用情况非常糟糕。

测试:为了测试此功能,我使用 Visual Studio 2012(面向 .NET 框架 4.5)创建一个带有以下代码的小型控制台应用程序。我正在跟踪一个 ~127MB 的静态文件。

问题:在功能上这工作正常,但内存使用情况非常糟糕。应用程序将在调用 shellStream.WriteLine 之前使用 ~7MB,然后快速增加并使用 ~144MB(当所有当前文件内容已从流中读取时稳定)。

下面是我尝试使用的代码。

private SshClient sshClient;
private ShellStream shellStream;
//Command being executed to tail a file.
private readonly string command = "tail -f -n+1 {0}";
//EventHandler that is called when new data is received.
public EventHandler<string> DataReceived;

public void TailFile(string server, int port, string userName, string password, string file)
{
   sshClient = new SshClient(server, port, userName, password);
   sshClient.Connect();

   shellStream = sshClient.CreateShellStream("Tail", 0, 0, 0, 0, 1024);

   shellStream.DataReceived += (sender, dataEvent) =>
   {
      if (DataReceived != null)
      {
         DataReceived(this, Encoding.Default.GetString(dataEvent.Data));
      }
   };

   shellStream.WriteLine(string.Format(command, file));
}

是否缺少一些东西来防止内存尽可能多地增加,或者任何其他可以实现相同目标的解决方案?

【问题讨论】:

    标签: c# ssh tail ssh.net


    【解决方案1】:

    您不使用流中的数据,因此它会累积。

    how the ShellStream.DataReceived event is implemented:

    private void Channel_DataReceived(object sender, ChannelDataEventArgs e)
    {
        lock (this._incoming)
        {
            // this is where the memory "leaks" as the _incoming is never consumed
            foreach (var b in e.Data)
                this._incoming.Enqueue(b);
        }
    
        if (_dataReceived != null)
            _dataReceived.Set();
    
        this.OnDataReceived(e.Data);
    }
    

    不要使用ShellDataEventArgs.Data,而是使用ShellStream.Read

     shellStream.DataReceived += (sender, dataEvent) =>
     {
        if (DataReceived != null)
        {
           DataReceived(this, shellStream.Read());
        }
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多