Getting error "Cannot allocate memory" for Rails

user2622247 picture user2622247 · Dec 3, 2013 · Viewed 10.1k times · Source

In my project there is one script that returns the list of products which I have to display in a table.

To store the input of the script I used IO.popen:

@device_list = []
IO.popen("device list").each do |device|
  @device_list << device
end

device list is the command that will give me the product list.

I return the @device_list array to my view for displaying by iterating it.

When I run it I got an error:

Errno::ENOMEM (Cannot allocate memory):
for IO.popen

I have on another script device status that returns only true and false but I got the same error:

def check_status(device_id)        
    @stat = system("status device_id")
    if @stat == true
         "sold"
    else
         "not sold"
    end
  end

What should I do?

Answer

Gjaldon picture Gjaldon · Dec 9, 2013

Both IO.popen and Kernel#system can be expensive operations in terms of memory because they both rely on fork(2). Fork(2) is a Unix system call which creates a child process that clones the parent's memory and resources. That means, if your parent process uses 500mb of memory, then your child would also use 500mb of memory. Each time you do Kernel#system or IO.popen you increase your application's memory usage by the amount of memory it takes to run your Rails app.

If your development machine has more RAM than your production server or if your production server produces a lot more output, there are two things you could do:

  1. Increase memory for your production server.
  2. Do some memory management using something like Resque.

You can use Resque to queue those operations as jobs. Resque will then spawn "workers"/child processes to get a job from the queue, work on it and then exit. Resque still forks, but the important thing is that the worker exits after working on the task so that frees up memory. There'll be a spike in memory every time a worker does a job, but it will go back to the baseline memory of your app every after it.

You might have to do both options above and look for other ways to minimize the memory-usage of your app.