2

I am experiencing a memory leak in a c# console application and I am at a bit of a loss as to what is causing it.

I did a dump of the application at various stages to compare how the heap changed. I noticed there were many pinned object references and quite a few ActiveMq objects in there also. Below is my ActiveMq consumer, producer and connection. I am using the latest Apache.NMS.ActiveMQ nuget package.

    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);
        }
    }

It is possible that the problem is coming from another part of the application entirely but it seems most likely to be activeMq at this point.

I wonder if anyone can spot anything that could cause the memory leak in my code? Let me know if I can give you any more information that could help!

  • can you show how `logger and config` are defined..? – MethodMan Dec 10 '15 at 14:32
  • Hi MethodMan, logger comes from library which is used in quite a few other places for quite some time in the same ways so would say it is unlikely to be that. Can include though if you would still like to see it. Config pulls from app.config like so: public interface IAdaptorConfig { [Setting("General.Environment")] string Environment { get; } Setting also comes from a fairly regularly used internal library. – Seán O Brien Dec 10 '15 at 15:17
  • Also both are injected in using DryIoc. Logger as a singleton: container.Register(Reuse.Singleton); – Seán O Brien Dec 10 '15 at 15:23
  • My advice is to use the latest release if you think there is a problem, think nuget has old releases, latest is 1.7.1 – Tim Bish Dec 10 '15 at 17:09
  • Hi Tim, 1.7.1 is the version I am using. Thanks – Seán O Brien Dec 14 '15 at 15:58

0 Answers0