On Thursday 13 May 2010 11:59:36 Akhthar Parvez K wrote:
> Hi Uri,
> >   APK> 3) Also, am I correct in guessing that the memory that's used to
> >   APK> allocate a variable defined with 'my' will be freed up once the
> >   APK> current lexical scope is exited?
> > 
> > true but with a file lexical (or global) that won't happen until the
> > program exits. this is another reason to declare things in the tightest
> > scope possible. also note that the memory is freed up for perl to reuse,
> > not for the OS. it will mean perl won't need to grow its size if it can
> > reuse lexical data that is reclaimed upon scope exit.
> 
> Doesn't Perl free up any memory that was used by it until the program
> exits? If that's how it goes, if a Perl program uses a lot of memory at
> the beginning and it takes lots of time for the program to finish up, all
> those memory space are being wasted, no?

Perl will be able to reuse this memory for other stuff, but as far as the 
operating system kernel is concerned, the memory is still held by the process. 
This happens in C programs too. If you do in C:

<<<
char * mem = malloc(1024);

# Do stuff with mem.

free(mem);
mem = NULL;
>>>

Then it is very likely that the memory allocated to the "mem" pointer will not 
be returned to the kernel due to the nature of malloc() and how it is an 
abstraction above sbrk():

http://linux.die.net/man/2/sbrk

perl 5 makes use of sbrk eventually as well (either by using the libc malloc() 
or by implementing its own). Note that for very large allocations, perl may 
use mmap() instead of sbrk() in which case the memory may get released. I've 
seen it in a demonstration program that someone pasted on IRC, a few years 
back.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to