amqp.cr icon indicating copy to clipboard operation
amqp.cr copied to clipboard

Memory leak in publish

Open velddev opened this issue 7 years ago • 1 comments

I'm trying to create a publish-only system to send messages to workers from a stable connection, however it's using too much ram to possibly keep it running.

one minute running with amqp.cr

I, [2018-07-04 23:22:11 +00:00 #24123]  INFO -- : heap: 1.100.210.176

another minute running without amqp.cr implemented

I, [2018-07-04 23:25:25 +00:00 #24721]  INFO -- : heap: 108.081.152

code used

host = ENV["AMQP_HOST"]
port = ENV["AMQP_PORT"].to_i32
user = ENV["AMQP_USER"]
pass = ENV["AMQP_PASS"]

conn = AMQP::Connection.new AMQP::Config.new host, port, user, pass

@@amqp : AMQP::Connection = conn
@@channel : AMQP::Channel = @@amqp.channel
@@exchange : AMQP::Exchange = @@channel.default_exchange

#callback (body)
msg = AMQP::Message.new body
@@exchange.publish msg, ENV["AMQP_EXCHANGE"]

velddev avatar Jul 04 '18 23:07 velddev

Just my two cents - but a disclaimer; I haven't used this library or an AMPQ service before.

@velddev brought this issue up to me first asking if I had any ideas, and a cursory glance over the source I noticed in places there are several types as class which are used in an immutable fashion so they should be changed to struct; specifically in protocol.cr where I imagine a lot of these low level internal types are instantiated frequently. This would change allocation to the stack instead of the heap, which should be faster as well(*)

I don't have the bandwidth to test or review more closely at the moment, but maybe this is a step in the right direction.

* "Avoiding memory allocations", Crystal language reference

z64 avatar Jul 05 '18 22:07 z64