Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]
[GotToBTru]: that sounds fair to me
[davies]: I've just "approved" using syscalls in perl through inline c for at least the third time, but it still appears to me as unapproved. Is this just me?
[LanX]: erix: other side of the pond
[LanX]: davies: same problem
[Corion]: It had been approved as a Perl Monks Discussion. It should now be approvable into SoPW
[choroba]: Approved
[LanX]: hmm aproval doesn't stick after move? Or was it a race condition

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-01-16 15:07 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.