in reply to perl memory usage

A better test would be this:

{ my $buf = "A" x (1024 * 1024 * 100); print "Allocated " . length($buf) . " byte buffer\n"; } print "Finished\n"; sleep(1000);

To make $buf go out of scope, but even then it still won't do what you expect.

Replies are listed 'Best First'.
Re^2: perl memory usage
by perrin (Chancellor) on Feb 04, 2008 at 12:59 UTC
    Actually, the undef is more likely to return memory. When a variable goes out of scope, Perl keeps the memory assigned to it in case you use it again. Calling undef on it returns that memory to the pool. However, it may or may not return the memory to the OS as seen here.
Re^2: perl memory usage
by awesley (Novice) on Feb 04, 2008 at 22:07 UTC
    G'day DrHyde - indeed I tried your suggestion, as well as several other variations before posting my question. In the end I settled on the code that seemed to express my question most clearly, not necessarily the code I would use :-) (Actually the code and question came to me from a colleague, and I was working variations on *his* code). But I still have a question - when the 100Mb string is allocated I see perl use 200Mb of ram, and after I have undef'd the scalar the memory use drops to 100Mb. I'm at a loss to explain this behaviour - why would the 200Mb drop to 100Mb? I would have thought it should either drop to nothing (if the buffer was released) or stay at 200Mb (if the buffer was not released).? regards, Anthony