thd
thd

Reputation: 2430

Serving 1GB file locally in public folder using Rails

I have a large file named large.zip (about 1GB) stored in the public folder. Whenever I try to download it from localhost (localhost:3000/large.zip), I receive the following error message

[2013-08-24 22:22:28] ERROR NoMemoryError: failed to allocate memory
        E:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/handler/w
ebrick.rb:72:in `block in service'
        E:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/file.rb:1
25:in `block in each'
        E:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/file.rb:1
17:in `open'
        E:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/file.rb:1
17:in `each'
        E:/RubyStack/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/handler/w
ebrick.rb:71:in `service'
        E:/RubyStack/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
        E:/RubyStack/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
        E:/RubyStack/ruby/lib/ruby/1.9.1/webrick/server.rb:191:in `block in star
t_thread'

The download stops at 43619924 bytes (about 400MB). My PC is 8GB RAM and I'm using ruby 1.9.3, rails 3.2.14 and webbrick as webserver. I think there is some limits on the size of allocated memory (400MB ?). So is there a way to increase this limit ? Or how to solve this problem ?

Edit: I just create a rails application using rails new then I copy the large.zip file into the public folder. Then I open the link localhost:3000/large.zip on Chrome to download it (again).

Upvotes: 2

Views: 620

Answers (1)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

Don't try to stream large files directly via Rails, especially with WEBrick as your server. Instead, use #send_file with a web server like Apache or Nginx that supports X-Sendfile. This enables the web server to stream the file directly without consuming large amounts of memory in your Rails process.

Upvotes: 5

Related Questions