http://www.perlmonks.org?node_id=1002160


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.