Python subprocess.Popen "OSError: [Errno 12] Cannot allocate memory"
Solution 1:
As a general rule (i.e. in vanilla kernels), fork
/clone
failures with ENOMEM
occur specifically because of either an honest to God out-of-memory condition (dup_mm
, dup_task_struct
, alloc_pid
, mpol_dup
, mm_init
etc. croak), or because security_vm_enough_memory_mm
failed you while enforcing the overcommit policy.
Start by checking the vmsize of the process that failed to fork, at the time of the fork attempt, and then compare to the amount of free memory (physical and swap) as it relates to the overcommit policy (plug the numbers in.)
In your particular case, note that Virtuozzo has additional checks in overcommit enforcement. Moreover, I'm not sure how much control you truly have, from within your container, over swap and overcommit configuration (in order to influence the outcome of the enforcement.)
Now, in order to actually move forward I'd say you're left with two options:
- switch to a larger instance, or
- put some coding effort into more effectively controlling your script's memory footprint
NOTE that the coding effort may be all for naught if it turns out that it's not you, but some other guy collocated in a different instance on the same server as you running amock.
Memory-wise, we already know that subprocess.Popen
uses fork
/clone
under the hood, meaning that every time you call it you're requesting once more as much memory as Python is already eating up, i.e. in the hundreds of additional MB, all in order to then exec
a puny 10kB executable such as free
or ps
. In the case of an unfavourable overcommit policy, you'll soon see ENOMEM
.
Alternatives to fork
that do not have this parent page tables etc. copy problem are vfork
and posix_spawn
. But if you do not feel like rewriting chunks of subprocess.Popen
in terms of vfork
/posix_spawn
, consider using suprocess.Popen
only once, at the beginning of your script (when Python's memory footprint is minimal), to spawn a shell script that then runs free
/ps
/sleep
and whatever else in a loop parallel to your script; poll the script's output or read it synchronously, possibly from a separate thread if you have other stuff to take care of asynchronously -- do your data crunching in Python but leave the forking to the subordinate process.
HOWEVER, in your particular case you can skip invoking ps
and free
altogether; that information is readily available to you in Python directly from procfs
, whether you choose to access it yourself or via existing libraries and/or packages. If ps
and free
were the only utilities you were running, then you can do away with subprocess.Popen
completely.
Finally, whatever you do as far as subprocess.Popen
is concerned, if your script leaks memory you will still hit the wall eventually. Keep an eye on it, and check for memory leaks.
Solution 2:
Looking at the output of free -m
it seems to me that you actually do not have swap memory available. I am not sure if in Linux the swap always will be available automatically on demand, but I was having the same problem and none of the answers here really helped me. Adding some swap memory however, fixed the problem in my case so since this might help other people facing the same problem, I post my answer on how to add a 1GB swap (on Ubuntu 12.04 but it should work similarly for other distributions.)
You can first check if there is any swap memory enabled.
$sudo swapon -s
if it is empty, it means you don't have any swap enabled. To add a 1GB swap:
$sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
$sudo mkswap /swapfile
$sudo swapon /swapfile
Add the following line to the fstab
to make the swap permanent.
$sudo vim /etc/fstab
/swapfile none swap sw 0 0
Source and more information can be found here.
Solution 3:
For an easy fix, you could
echo 1 > /proc/sys/vm/overcommit_memory
if you're sure that your system has enough memory. See Linux over commit heuristic.