我在c#控制台应用程序中遇到内存泄漏问题,我对它造成的影响有点不知所措。
我在不同阶段对应用程序进行了转储,以比较堆的更改方式。我注意到有许多固定的对象引用和相当多的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。
我想知道是否有人能发现任何可能导致代码中内存泄漏的内容?如果我能提供更多可以提供帮助的信息,请告诉我们。