shooter714 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I need to use the IPC::Open2 module to write data into and retrieve data from another program inside a perl script. I need to do this many many times, but after awhile I get the error message:

open2: fork failed: cannot allocate memory at line whatever

My question is: is this some shell error and is it fixable or does the system on my computer just not have enough RAM to complete the program? Or something else? I have a line to delete child processes once I'm done with them so they don't turn into zombies btw. Thanks in advance.

Replies are listed 'Best First'.
Re: cannot allocate enough memory to fork
by jethro (Monsignor) on Apr 10, 2010 at 01:45 UTC

    Without looking at your script it is hard to tell what is using up all the memory. You should know better: Do you have so much data that it would fill your RAM?

    If not, could it be that you don't delete child processes correctly?

    You could (if you are on linux or some other unixoide) type "ps -elf" repeatedly in a shell while the script runs to check if either one process gets too big or too many processes are created and are not removed.

Re: cannot allocate enough memory to fork
by graff (Chancellor) on Apr 10, 2010 at 04:35 UTC
    Do you need to have "many many" processes running concurrently? If not, are you sure that all those processes are being shut down and cleaned up (not being left as zombies, not hanging around waiting for more input, etc)? (OTOH, if you think you do need lots of processes running concurrently, you'll need to explain your situation a little better.)

    The "memory allocation" problem might be with respect to some particular property of your OS (like, space available for process management), but that's just a guess. I'm also guessing that the code at "line whatever" may be the least of your problems.

Re: cannot allocate enough memory to fork
by BrowserUk (Patriarch) on Apr 10, 2010 at 01:38 UTC

    SUTC!

    (Maybe a candidate for a reference node: Show Us the Code!?)

Re: cannot allocate enough memory to fork
by cdarke (Prior) on Apr 10, 2010 at 13:36 UTC
    is this some shell error

    The only relevance I can see of a shell is that you could check the ulimit settings.

    You don't state which operating system, but I assume some sort of *nix. UNIX is a virtual memory operating system, and RAM is only one of the memory resources. More likely though you are hitting a kernel limit setting. UNIX and Linux have hard and soft limits on memory allocated for user processes and (sometimes) for kernel. You might be hitting one of those limits.

    Its a long shot, but have you checked the administrator's log files (like /var/log/messages) for similar errors? They might point you in the right direction.