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

How to get the amount of RAM (kbs) in perl?

Thx.

Replies are listed 'Best First'.
Re: How to get the amount of RAM
by BrowserUk (Patriarch) on Nov 08, 2011 at 18:19 UTC
    How to get the amount of RAM (kbs) in perl?

    The amount of ram:

    1. Used by a variable?
    2. Used by the program?
    3. In the machine?
    4. Free for use?
    5. Accessible to the program?
    6. On the planet?
    7. Other?

    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.

      6. On the planet?

        There is a problem calculating this one.

        There doesn't seem to be any reliable record of how much RAM has been launched into space and so is no longer technically on the planet.


        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.
      3. In the machine.

        On windows, see Win32::SystemInfo.


        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.

      well, this sucks as a perl solution, but... in kbs:

      print `free -l -t`;

      if you prefer the output to be in megas, you can add -m to the command

Re: How to get the amount of RAM
by jffry (Hermit) on Nov 08, 2011 at 18:55 UTC

    Here is one way to find the amount of memory used by the entire Perl process on Unix systems.

    me@mybox:~/sandbox $ perl -e 'while (1) {}' & [1] 28892 me@mybox:~/sandbox $ ps -o vsz -p 28892 VSZ 77916

    That's 77 MB used in that perl process.

      I usually find rss and sz more useful comparing to vsz

        And for the ram used by perl you can do this also...

        print `memstat |grep perl`;

        you can ask for a specific process also, following the former example

        print `memstat -p 28892`; __END__ 592k: PID 5273 (/usr/bin/perl) 116k( 108k): /lib/ld-linux.so.2 28892 92k( 84k): [08:01]:258781 28892 44k( 36k): [08:01]:258783 28892 16k( 8k): [08:01]:258784 28892 152k( 144k): [08:01]:258791 28892 1372k( 1356k): [08:01]:258796 28892 1404k( 1396k): /usr/bin/perl 28892 1500k( 0k): [08:06]:242490 28892 -------- 5288k ( 3132k)

        for memory use inside a specific part of a perl script see also malloc() and memstat() or -DL and warn('!') marks