Tomas Aleksiejunas
Tomas Aleksiejunas

Reputation: 41

Memory leak in ActionMailer

I have the following code. It is an ActionMailer class method which sends email containing two kinds of attachments:

  1. pdf file (_attachment), rendered in memory and added directly to the message

  2. some other files (_attached_files), that can be added from file system.

Everything works perfectly, except one thing - it leaks memory. As long as users send messages with attachments, memory consumption keeps growing and and growing. It does not return memory back. As far as I tested, I suspect that this problem is related to attached files from the file system, not rendered PDF file.

  attachments[_attachment.pdf_filename] = render(_attachment.pdf_template_path, :format => :pdf)

 _attached_files.try(:each) do |file|
      attachments[file.attachment_file_name] = File.read(file.attachment.path, mode:"rb")
 end

mail(:to =>_recipients, :from=>_sender_name, :subject => _subject)

Upvotes: 3

Views: 609

Answers (2)

Tomas Aleksiejunas
Tomas Aleksiejunas

Reputation: 41

Oh, finally I have found the cause of the memory leak. I was wrong - it was related to the pdf file.

I use prawn to render pdf files. I included incorrectly external font families and used them in the tables. Each time prawn generated table some amount of memory remained allocated.

Upvotes: 1

RadBrad
RadBrad

Reputation: 7304

I do a similar thing, except I do

  attachments[file.attachment_file_name] 
     = File.open(file.attachment.path, "rb") {|f| f.read}

I'm not sure if this is your problem, but it's worth a try

Upvotes: 0

Related Questions