How to solve the memory-leak with send_file (o

2019-08-11 05:55发布

I have a Rails 3 app that needs to generate an image and send the data to the browser.

The app must be deployed on Heroku.

However, Heroku only supports streaming through Mongrel which holds on to the memory. This then causes Heroku to slow, then kill the thread after a dozen or so requests.

https://devcenter.heroku.com/articles/error-codes#r14-memory-quota-exceeded

I am currently using send_data or send_file from ActionController::DataStreaming

http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data

Heroku does not support Rack::Sendfile or x-sendfile.

https://devcenter.heroku.com/articles/rack-sendfile

The project "ruby-mongrel-x-sendfile" says: "Streaming very much data through mongrel is a bad thing; springs stringy memory leaks" and provides an "in-mongrel solution". But it doesn't look like a good solution.

http://code.google.com/p/ruby-mongrel-x-sendfile/

A slow solution to this is to upload every file to Amazon S3 first.

Does anyone have any ideas please?

1条回答
唯我独甜
2楼-- · 2019-08-11 06:30

The answer is to start garbage collection with:

GC.start

I placed that line at the bottom of the Rails controller action after send_data.

http://www.ruby-doc.org/core-1.9.3/GC.html

查看更多
登录 后发表回答