Dienstag, 24. Februar 2009

Perl Memory Consumption

Perl Memory, the Perl Garbage Collector


A small proposal to early free some Memory allocated in Perl:

Garbage Collector: automatic undef on scope closure



iff -urN perl-5.8.8/scope.c perl-5.8.8_/scope.c
--- perl-5.8.8/scope.c 2005-09-30 15:56:51.000000000 +0200
+++ perl-5.8.8_/scope.c 2009-02-23 16:23:40.000000000 +0100
@@ -946,14 +946,26 @@
SvREFCNT_dec(AvARYLEN(sv));
AvARYLEN(sv) = 0;
}
+ av_undef((AV*)sv);
break;
case SVt_PVHV:
hv_clear((HV*)sv);
+ hv_undef((HV*)sv);
break;
case SVt_PVCV:
Perl_croak(aTHX_ "panic: leave_scope pad code");
default:
SvOK_off(sv);
+ const U32 padflags
+ = SvFLAGS(sv) & (SVs_PADBUSY|SVs_PADMY|SVs_PADTMP);
+ switch (SvTYPE(sv)) { /* Console ourselves with a new value */
+ case SVt_PVAV: *(SV**)ptr = (SV*)newAV(); break;
+ case SVt_PVHV: *(SV**)ptr = (SV*)newHV(); break;
+ default: *(SV**)ptr = NEWSV(0,0); break;
+ }
+ SvREFCNT_dec(sv); /* Cast current value to the winds. */
+ SvFLAGS(*(SV**)ptr) |= padflags; /* preserve pad nature */
+
break;
}
}


So yes, I have patched the file scope.c. Because a lot of the (existing) code does not use a explicit undef on a scope exit (of course, does this imply a variable declared with 'my').
At least it helps me to free some malloced space on a scope close (see below).
After applying this patch you can do such thing:


print "Start: >".$$."<\n";<>;
{
my @arr;
$arr[$_]=$_ foreach(1 .. 1000000);
print "How Many?\n";<>;
# undef @arr;
}
print "Done, how many?\n";<>;

And you recognize, that there is now a automatic undefining on scope closure.

References: Perlmonks


A discussion on Perlmonks about the Perl Garbage Collector

References


Votrag: Perl Speicherverbrauch

Keine Kommentare: