>>>>> "JG" == Jim Green <student.northwest...@gmail.com> writes:

  JG> I have a quick question about memory release in perl:

  JG> {
  JG>     my @array;

  JG>     foreach my $n (1..1e7 ) {
  JG>         push @array, $n;
  JG>         print "$n\n";
  JG>     }
  JG> }

someone asked about the original code. this is a silly example in
several ways. it is building up an array with just the indexes as values
(actually off by one but that doesn't matter here). so it it not worth
fixing as it is just an example of large ram usage for no reason.

as i said in another post, @array will be freed up at the block exit but
it won't be freed back to the OS. the process will own that ram until it
exits. but perl will reuse that ram before it allocates more which is
usually fine for most programs.

there is no direct way to reduce the ram usage in that code. it is just
designed to suck up ram just to see it being done. you couldn't free
that up in most any lang as none have a way to free it back to the
OS. you can do that in c IF and only IF you don't call malloc/free and
you call brk/sbrk yourself and manage your own ram. it is extremely
difficult for a lang to do that for you as allocation and freeing of ram
will be random and not controlled so you can't free the ram to the
OS. you can only free the top contiguous ram to the OS with a brk
call. this gets into seriously tricky stuff that i won't get into more
here as it won't affect the perl code.

if you have a more realistic example with large ram usage, that possibly
could be addressed. even so, this is not a typical beginner topic.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
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