Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.


Comment on Re: How can I make my program occupying a lot of memory (sic!)?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002160]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-08-31 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls