Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: Timing of garbage collection

by Marshall (Prior)
on Jan 19, 2013 at 06:40 UTC ( #1014175=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Timing of garbage collection
in thread Timing of garbage collection

I think that it is fair to say that "mileage varies" even in this simple test....

In a running application, things get more complicated than just "give me a MB" and then immediately I give you back a MB. Over time memory typically becomes more and more fragmented and it becomes more difficult to give back a "useable chunk" of memory...mileage does vary. And this does depend upon the malloc() that is being used. And of course the code.

My original statement was not completely accurate. Sorry about that.

C:perl -E"say `tasklist|find \"$$\"`; $x=chr(0); $x x= 1040352; say ` +tasklist|find \"$$\"`; undef $x; say `tasklist|find \"$$\"`" perl.exe 2208 Console 0 2,556 K perl.exe 2208 Console 0 3,600 K perl.exe 2208 Console 0 2,584 K C:perl -E"say `tasklist|find \"$$\"`; $x=chr(0); $x x= 1040352; say ` +tasklist|find \"$$\"`; undef $x; say `tasklist|find \"$$\"`" perl.exe 576 Console 0 2,552 K perl.exe 576 Console 0 3,592 K perl.exe 576 Console 0 2,576 K C:perl -E"say `tasklist|find \"$$\"`; $x=chr(0); $x x= 1040352; say ` +tasklist|find \"$$\"`; undef $x; say `tasklist|find \"$$\"`" perl.exe 5712 Console 0 2,556 K perl.exe 5712 Console 0 3,600 K perl.exe 5712 Console 0 2,584 K C:perl -E"say `tasklist|find \"$$\"`; $x=chr(0); $x x= 1040352; say ` +tasklist|find \"$$\"`; undef $x; say `ta sklist|find \"$$\"`" perl.exe 5468 Console 0 2,552 K perl.exe 5468 Console 0 3,596 K perl.exe 5468 Console 0 2,580 K


Comment on Re^5: Timing of garbage collection
Download Code
Re^6: Timing of garbage collection
by BrowserUk (Pope) on Jan 19, 2013 at 17:21 UTC

    First, let me say that I was not arguing with the basic premise that in general, Perl does not give memory back to the OS. Only that the definitive statement (in common with most definitive statements), that perl *never* gives memory back to the OS, is not strictly true.

    I think that it is fair to say that "mileage varies" even in this simple test....

    Hm. If by that you mean that the exact numbers of the recorded memory allocation can vary a little. Yes, of course.

    You're running the tests on a system (OS; file-cache; virtual memory; background processes and device drivers et al.) -- that is ever in flux, but that does not detract from the fact that (on AS Perl on windows), if you make single allocations of greater than 1 MB, when those allocations are freed, they will be returned to the OS. They will never be fragmented, because they will never be reallocated for anything else.

    This doesn't just apply to large scalars either. For example, if you create an array where the base AV requires more than 1 MB of contiguous memory, when that array is freed, that contiguous chunk for the AV gets returned to the OS.

    Here I preallocate the AV for an array to hold upto 1 million values which on my 64-bit perl requires an 8*1e6 = 8000000 byte chunk of contiguous memory:

    C:\test>perl -E"say `tasklist|find \"$$\"`; $#a = 1e6; say `tasklist|f +ind \"$$\"`; undef @a; say `tasklist|find \"$$\"`" perl.exe 247600 Console 1 4 +,676 K perl.exe 247600 Console 1 12 +,552 K perl.exe 247600 Console 1 4 +,720 K

    And when the array is freed, that ~8MB is given back.

    And if I populate that array:

    C:\test>perl -E"say `tasklist|find \"$$\"`; $#a = 1e6; $a[$_]=$_ for 0 +..1e6-1; say `tasklist|find \"$$\"`; undef @a; say `tasklist|find \"$ +$\"`" perl.exe 293992 Console 1 4 +,688 K perl.exe 293992 Console 1 36 +,872 K perl.exe 293992 Console 1 29 +,044 K

    Although the additional 24MB of ram (1e6 * 24B) required to hold the array's scalars is not returned to the OS, the 8MB allocated to the AV, *still is*.

    The difference is that the 1e6 x 24 byte NVs are allocated from the process memory pool on the fly, and that pool is expanded -- in smallish chunks -- on the fly to accommodate them. Thus those piecemeal allocations are not contiguous chunks greater than 1MB, so do not get returned to the OS.

    This knowledge is arcane and system dependent, but with care (and the need) it can be exploited to good effect if you use a system where it is so.

    Just beware of overly simplistic, 'definitive truths'.


    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.
      I think that this is correct.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-07-22 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (107 votes), past polls