大虾PDF在Heroku Rails应用中导致内存膨胀

时间:2019-01-08 15:30:26

标签: ruby-on-rails email pdf heroku memory

每当生成PrawnPDF时,我的Heroku Rails应用程序都会使内存膨胀。我为每一代使用以下代码。 使用方法: Ruby 2.5.3 虾2.2.2 大虾表0.2.2

pdf = InvoicePdf.new(hash)
send_data pdf.render, filename: "receipt_#{order_id}.pdf"

在我的邮件中,作为附件:

pdf = InvoicePdf.new(hash)
t = Tempfile.create do |f|
  pdf.render_file f
  f.flush
  File.read(f)
end
attachments["receipt.pdf"] = t if t

以上代码源自此处:Attach Prawn pdf to email

这两种方法都会导致内存膨胀。我见过的唯一解决方案是使用AWS / cloudinary存储pdf。我只会使用pdf两次(而且都没有时间压力),所以这似乎是浪费。有什么想法吗?

我在启动时没有记忆体问题。

谢谢, 盖伊

1 个答案:

答案 0 :(得分:3)

以防万一将来有人在看这个-Ruby GC看起来很混乱。只需取消引用pdf(pdf = nil)即可确保已被垃圾回收。