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!