使用ActiveMQ-cpp时,使用Publish
创建和发送消息的所有ActiveMQ客户端都会逐渐增加内存使用量。现在看起来每个消息发送大约4Kb。 valgrind似乎没有任何内存泄漏,内存增加将继续,直到程序终止或使用所有可用的系统内存。
当消息被发送而没有被任何其他ActiveMQ客户端接收并且消息只是由生产者发送而没有其他消费者时,会发生内存增加。似乎创建生产者的行为可以导致内存增加。以下是对session_
的调用的示例代码,该代码导致内存增加。我还尝试使用成员void ActiveMqClient::Publish(std::string type,
void* input, size_t len) {
if(type == "") {
ead::eadwarn() << "ActiveMqClient::Publish() - Attempting to publish to "
"empty string topic. Please check your message topic." << std::endl;
}
cms::Session* session = connection_->createSession(
cms::Session::AUTO_ACKNOWLEDGE);
//creates a destination and producer
cms::Destination* destination(session->createTopic(type));
cms::MessageProducer* producer(session->createProducer(destination));
producer->setDeliveryMode(cms::DeliveryMode::PERSISTENT);
//creates message and sets properties
std::unique_ptr<cms::BytesMessage> message(session->createBytesMessage());
//gets byte array from input
size_t size_to_write = 0;
unsigned char* body = (unsigned char*) input;
if(io_handler_ != nullptr) {
body =
io_handler_->ConvertBodyForPublish(type, input, &len, &size_to_write);
}
//writes the bytes of input
message->writeBytes(const_cast<const unsigned char*>(body), 0,
size_to_write);
//gets byte array from input
unsigned char* payload = (unsigned char*) input;
if(io_handler_ != nullptr) {
payload = io_handler_->ConvertPayloadForPublish(type,
input,
len,
&size_to_write);
}
//writes the bytes of input
if (size_to_write != 0) {
message->writeBytes(payload, 0, size_to_write);
}
//sets the message type of the message
message->setStringProperty("MsgType", type);
//sets the message size
message->setIntProperty("size", len);
//sets the byte pointer to the beginning of the byte array
message->reset();
producer->send(message.get());
//calls sentcallback if it exists
if(io_handler_ != nullptr) {
io_handler_->HandleMessageSent(type, reinterpret_cast<char*>(body), len);
}
//clears memory
delete producer;
delete destination;
delete session;
}
变量来创建目标和生成器,而不是每次都创建一个新会话。
MessageProducer
因此,当以这种方式使用{{1}}时,关于为什么内存会稳定地增加的任何想法。无论我如何使用这种模式,它似乎都在不断增加内存使用量。提前感谢您对此的任何帮助!