首页 > 解决方案 > RabbitMq AutorecoveringChannels 被收集在内存中,即使它们在代码中被关闭

问题描述

在我的 RabbitMQ RPC 应用程序中,每发布一条消息,我都使用一个 RabbitMQ 连接和通道。因此,一旦消息得到结果响应,通道就会关闭。非常简单的应用程序。

即使我关闭了通道(我监控了运行时日志并确保所有通道都已关闭),但 AutorecoveringChannel 对象实例仍被收集并在内存中增长。我不确定为什么会这样。因为我希望 RabbitMQ 客户端在通道关闭后擦除所有数据和通道。

我在谷歌和任何地方搜索都知道为什么,但每篇文章都在告诉我要关闭频道。对于一个想法,请查看这个 VisualVM 结果的增长对象的屏幕截图。

 Object                                               instances

 com.rabbitmq.client.impl.recovery.AutorecoveringChannel    249
 com.rabbitmq.client.impl.recovery.RecordedQueue            502
 com.rabbitmq.client.impl.recovery.RecordedQueueBinding     502
 com.rabbitmq.client.impl.recovery.RecoveryAwareChannelN    255

这就是我在通过网络套接字接收到的每条消息中创建通道的方式

    Channel channel = rabbitMqController.getRmqConnection().createChannel();

    //some code

    DefaultConsumer consumer = getConsumer(methodCall, channel, callbackFunction);

    //some code

    channel.basicConsume(replyQueueName, true, consumer);
    channel.basicConsume(statusReplyQueueName, true, consumer);
    channel.basicPublish(WorkerConfig.getInstance().getWorkerExchangeName(), methodCall.getMethod(), true, props,
            methodCall.toJson());

在 getConsumer() 中,一旦消息响应,我就会像这样关闭频道。

        return new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                   byte[] body) throws IOException {

            closeChannel(channel);
            //handling code goes here
        }

closeChannel 就是 channel.close()。

如果您能猜到这里发生的任何事情,请与我分享您的想法。即使是小的建议也会让我在这个内存问题上走上正轨,我们很高兴欢迎。

标签: javarabbitmq

解决方案


推荐阅读