【问题标题】:Rabbitmq, IModel memory leak. c# .net frameworkRabbitmq,IModel 内存泄漏。 c# .net框架
【发布时间】:2020-07-20 10:48:49
【问题描述】:
var factory = new ConnectionFactory
{
    HostName = "localhost",
    UserName = "guest",
    Password = "guest",
    VirtualHost = "/",
    RequestedHeartbeat = TimeSpan.FromMilliseconds(10),
    AutomaticRecoveryEnabled = true,
    TopologyRecoveryEnabled = true,
    NetworkRecoveryInterval = TimeSpan.FromMilliseconds(500),
};
var connection = factory.CreateConnection();

for(int i = 0; i < 1000; ++i)
{
    using (IModel channel = connection.CreateModel())
    {
        var arguments = new Dictionary<String, Object>
                        {
                            {"x-message-ttl", 60000},
                            {"x-max-length", 5000}
                        };


        string exchangename = "oleg_exchange";
        string queuename = "oleg_queue";
        channel.ExchangeDeclare(exchangename, "direct", true, false, arguments);
        channel.QueueDeclare(queuename, true, false, true, arguments);
        channel.BasicQos(0, 1, false);
        channel.QueueBind(queuename, exchangename, "receipt", arguments);
    }
}

memory dump 在这里您可以看到 AutorecoveringModel 恰好创建了 5000 个对象,但必须销毁对象。

但是如果你关闭连接,那么一切都会被清除。

connection.Close();

这是正常行为吗?

【问题讨论】:

    标签: c# memory rabbitmq


    【解决方案1】:

    是的,这不是内存泄漏,因为您正在创建多个通道,每个通道都在内存中单独管理。但是,根据指南,不建议打开多个通道,也不需要每次操作都打开通道。建议根据用例重新访问代码。

    【讨论】:

    • 如果我需要处理 1,000,000 个请求会怎样?据我所知,没有必要重复使用频道。您只能在最新版本的 rabbitmq 中重用通道,因为通道在那里是线程安全的,但在早期版本中却不是。
    【解决方案2】:
    channel.QueueBind(queuename, exchangename, "receipt", arguments);
    

    这里不需要添加参数。 虽然这是rabbitmq中的一个错误。 如果您从此处删除参数,则不会发生内存泄漏

    channel.QueueBind(queuename, exchangename, "receipt");
    

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 2022-12-06
      • 1970-01-01
      • 2011-01-05
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多