Search code examples
ruby-on-railsimagemagickpaperclip

Rails app "Cannot allocate memory" when saving photo using Paperclip


I'm able to upload images in development on my local machine just fine, and occasionally an upload will fail when it's up on the server.

My setup is the rails droplet on DigitalOcean, pretty unchanged. That is, using Nginx and Unicorn.

This is the last part of the log:

I, [2014-06-15T07:19:14.456577 #17704]  INFO -- : Started GET "/photos" for 99.28.124.182 at 2014-06-15 07:19:14 +0000
I, [2014-06-15T07:19:14.458123 #17704]  INFO -- : Processing by PhotosController#index as HTML
I, [2014-06-15T07:19:14.537837 #17704]  INFO -- :   Rendered photos/index.html.erb within layouts/application (76.7ms)
I, [2014-06-15T07:19:14.540100 #17704]  INFO -- : Completed 200 OK in 82ms (Views: 78.7ms | ActiveRecord: 1.0ms)
I, [2014-06-15T07:19:23.700892 #17645]  INFO -- : Started GET "/photos/new" for 99.28.124.182 at 2014-06-15 07:19:23 +0000
I, [2014-06-15T07:19:23.703720 #17645]  INFO -- : Processing by PhotosController#new as HTML
I, [2014-06-15T07:19:23.717503 #17645]  INFO -- :   Rendered photos/_form.html.erb (8.1ms)
I, [2014-06-15T07:19:23.717913 #17645]  INFO -- :   Rendered photos/new.html.erb within layouts/application (8.9ms)
I, [2014-06-15T07:19:23.720011 #17645]  INFO -- : Completed 200 OK in 16ms (Views: 11.7ms | ActiveRecord: 0.5ms)
I, [2014-06-15T07:27:53.659373 #17704]  INFO -- : Started POST "/photos" for 99.28.124.182 at 2014-06-15 07:27:53 +0000
I, [2014-06-15T07:27:53.661330 #17704]  INFO -- : Processing by PhotosController#create as HTML
I, [2014-06-15T07:27:53.661460 #17704]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"[removed]", "photo"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x0000000138eba8 @tempfile=#<Tempfile:/tmp/RackMultipart20140615-17704-kpatwj>, @original_filename="IMG_0078.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"IMG_0078.jpg\"\r\nContent-Type: image/jpeg\r\n">, "description"=>"", "show_on_slideshow"=>"0", "show_on_gallery"=>"0"}, "commit"=>"Create Photo"}
I, [2014-06-15T07:27:53.671946 #17704]  INFO -- : Command :: file -b --mime-type '/tmp/91ff30d4faf0a3803148a98cb7ae142420140615-17704-f93sr9'
I, [2014-06-15T07:27:53.673477 #17704]  INFO -- : Completed 500 Internal Server Error in 12ms
F, [2014-06-15T07:27:53.676652 #17704] FATAL -- :
Errno::ENOMEM (Cannot allocate memory - file -b --mime-type '/tmp/91ff30d4faf0a3803148a98cb7ae142420140615-17704-f93sr9'):
app/models/photo.rb:14:in `check_file_size'
app/controllers/photos_controller.rb:32:in `create'

It is referring to line 32 which the @photo = Photo.new(photo_params) line here:

# POST /photos
# POST /photos.json
def create
  hide_from_unauthenticated
  @photo = Photo.new(photo_params)

  respond_to do |format|
    if @photo.save
      format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
      format.json { render :show, status: :created, location: @photo }
    else
      format.html { render :new }
      format.json { render json: @photo.errors, status: :unprocessable_entity }
    end
  end
end

Solution

  • Have you checked your current memory usage on your server? This issue often occurs when there are only a little of memory left. As you said, this does not happen all the time so I think lack of memory is the main reason.

    If you can not add more memory right away to your server, try adding a swap partition on your server can also be helpful.