Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "Cannot allocate memory" for Rails

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?

like image 330
user2622247 Avatar asked Dec 06 '22 04:12

user2622247


1 Answers

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.

like image 117
Gjaldon Avatar answered Dec 17 '22 20:12

Gjaldon