1

我在 ac# 控制台应用程序中遇到内存泄漏,我不知道是什么原因造成的。

我在不同阶段对应用程序进行了转储,以比较堆的变化情况。我注意到那里有很多固定的对象引用和相当多的 ActiveMq 对象。下面是我的 ActiveMq 消费者、生产者和连接。我正在使用最新的 Apache.NMS.ActiveMQ nuget 包。

    public ActiveMqClient(ILogger logger, IAdaptorConfig config)
    {
        try
        {
            _config = config;
            _logger = logger;

            var connectionFactory = new ConnectionFactory(_config.ConnectionString);
            _connection = connectionFactory.CreateConnection(_config.Username, _config.Password);
            _connection.RequestTimeout = TimeSpan.FromSeconds(3);
            _session = _connection.CreateSession();
            _connection.Start();
            _logger.Info(string.Format("Successfully created a connection to ActiveMq with client id:{0}, timeout {1} and prefetch policy {2}", _connection.ClientId, _connection.RequestTimeout, connectionFactory.PrefetchPolicy.QueuePrefetch));
        }
        catch (Exception e)
        {
            _logger.Error(string.Format("Failed to create connection to messaging service using connection string {0}, Exception: {1}", _config.ConnectionString), e.Message);
        }
    }

    public void Subscribe()
    {
        try
        {
            var dest = _session.GetDestination(_config.InQueue);
            _consumer = _session.CreateConsumer(dest);
            _consumer.Listener += OnMessage;
            _logger.Info(string.Format("Subscribed to activeMQ queue {0}", _config.InQueue));
        }
        catch (Exception ex)
        {
            _logger.Error(string.Format("Failed to subscribe to {1} . ex: {0}", ex.Message, _config.InQueue));
        }
    }

    public void SendMessage(string message)
    {
        try
        {
            using (var dest = _session.GetDestination(_config.OutQueue))
            using (var producer = _session.CreateProducer(dest))
            {
                producer.RequestTimeout = TimeSpan.FromSeconds(3);
                var request = GetSession().CreateXmlMessage(message);
                request.Text = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + message;
                producer.Send(request);
                _logger.Info(string.Format("Created new producer and sending message to {0}", _config.OutQueue));
            }
        }
        catch (Exception e)
        {
            _logger.Error("Problem sending message", e);
        }
    }

问题可能完全来自应用程序的另一部分,但此时似乎最有可能是 activeMq。

我想知道是否有人能在我的代码中发现任何可能导致内存泄漏的东西?让我知道我是否可以为您提供更多有用的信息!

4

0 回答 0