使用 ActiveMQ-cpp 时,所有创建并使用发送消息的 ActiveMQ 客户端会cms::MessageProducer
逐渐增加内存使用量。现在看起来每条消息发送大约 4Kb。valgrind 似乎没有任何内存泄漏,并且内存增加将继续,直到程序终止或使用所有可用的系统内存。
当消息被发送但没有被任何其他 ActiveMQ 客户端接收,以及消息只是由生产者发送而没有其他消费者时,内存会增加。似乎创建生产者的行为也会导致内存增加。Publish
这是导致内存增加的调用示例代码。我也尝试过只使用一个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
想法。无论我如何使用这种模式,它似乎都在不断增加内存使用量。提前感谢您对此的任何帮助!