Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: How can I make my program occupying a lot of memory (sic!)?

by rjt (Deacon)
on Nov 04, 2012 at 03:00 UTC ( #1002160=note: print w/replies, xml ) Need Help??

in reply to How can I make my program occupying a lot of memory (sic!)?

POSIX fork() itself will not (significantly) increase overall memory usage. If the parent uses 1GiB of memory and fork()s, the child will use the same 1GiB worth of pages in the operating system. If you go ahead and change any part of that memory in the child or parent, the operating system will make a copy of the affected page(s) of virtual memory.

This is known as copy-on-write (CoW) memory.

In your example code (which differs in an important way from your question about fork()), you exec another system command via backticks, which actually invokes a new shell and starts a fresh process from that shell rather than directly from your program. The implied exec() here essentially creates a new process image. In this case the OS has nothing to copy, and hence will not be able to take advantage of CoW and will be forced to allocate fresh pages for all of the runtime memory you request, because your program asks it to. In other words, if the memory has already been allocated and initialized pre-fork(), it will remain so post-fork(), in both the child and parent process.

None of this is at all new or Perl-specific, by the way, so reading up on the underlying fork(2) and exec(3) manual pages on any POSIX system would probably provide some useful background.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002160]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-02-20 20:10 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.