Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Doctrin (Beadle)
on Nov 03, 2012 at 20:06 UTC ( [id://1002138]=perlquestion: print w/replies, xml ) Need Help??

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

I just want to perform a test, that answers a question: How much resources would be occupied totally if I run following code from a program that occupies, say, 1G of memory itself? Here is the code:
unless (fork) { unless (fork) { `myScript.pl PARAM1 PARAM2 PARAM3`; exit 0; } exit 0; } wait;
myScript.pl itself occupies a little of resources. I'm just afraid that when I fork() a program that occupies 1G of memory it would lead to elseone 1G of memory occupied. Could anyone tell me either how to make a program occupy a lot of memory or answer a question above? Thanks in advance. PS I work under Linux

Replies are listed 'Best First'.
Re: How can I make my program occupying a lot of memory (sic!)?
by BrowserUk (Patriarch) on Nov 03, 2012 at 20:25 UTC
    it would lead to elseone 1G of memory occupied.

    ??

    Could anyone tell me either how to make a program occupy a lot of memory

    Easy:

    my $mem = chr(0); $mem x= 1024**3; ## 1 GiB.
    or answer a question: How much resources would be occupied totally if I run following code from a program that occupies, say, 1G of memory itself? ]

    Try it :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Hmm, no, I've made a mistake. It uses an extra 1 GiB as free -m says. Anyways, thanks for the code :) Now I know that I cannot execute fork()-s from heavy programs.
        It uses an extra 1 GiB as free -m says.

        I don't use *nix, but I think that the tool you are using is giving you bad info.

        AFAIK: That memory will not be cloned unless the forked child accesses it.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

      Thanks a lot! This is what I've needed. I tried to launch your code, then fork, and it didn't take much more memory. I just thought that fork() call would duplicate the memory related to the parent process, but it didn't as it seems...
Re: How can I make my program occupying a lot of memory (sic!)?
by rjt (Curate) on Nov 04, 2012 at 03:00 UTC

    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1002138]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-03-19 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found