Errno::ENOMEM: Cannot allocate memory - cat

最后都变了- 提交于 2019-12-04 23:39:43

So it seems that your system is running pretty low on memory and spawning a shell + calling cat is too much for the few memory left.

If you don't mind loosing some speed, you can merge the files in ruby, with small buffers. This avoids spawning a shell, and you can control the buffer size.

This is untested but you get the idea :

buffer_size = 4096
output_file = File.open(final_output_file, 'w')

Dir["#{processing_directory}/*.csv"].sort_by {|file| [file.count("/"), file]}.each do |file|
  f = File.open(file)
  while buffer = f.read(buffer_size)
    output_file.write(buffer)
  end
  f.close
end
kenorb

You are probably out of physical memory, so double check that and verify your swap (free -m). In case you don't have a swap space, create one.

Otherwise if your memory is fine, the error is most likely caused by shell resource limits. You may check them by ulimit -a.

They can be changed by ulimit which can modify shell resource limits (see: help ulimit), e.g.

ulimit -Sn unlimited && ulimit -Sl unlimited

To make these limit persistent, you can configure it by creating the ulimit setting file by the following shell command:

cat | sudo tee /etc/security/limits.d/01-${USER}.conf <<EOF
${USER} soft core unlimited
${USER} soft fsize unlimited
${USER} soft nofile 4096
${USER} soft nproc 30654
EOF

Or use /etc/sysctl.conf to change the limit globally (man sysctl.conf), e.g.

kern.maxprocperuid=1000
kern.maxproc=2000
kern.maxfilesperproc=20000
kern.maxfiles=50000
unixs

I have the same problem, but instead of cat it was sendmail (gem mail).

I found problem & solution here by installing posix-spawn gem, e.g.

gem install posix-spawn

and here is the example:

a = (1..500_000_000).to_a

require 'posix/spawn'
POSIX::Spawn::spawn('ls')

This time creating child process should succeed.

See also: Minimizing Memory Usage for Creating Application Subprocesses at Oracle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!