[kansaipm] Perlでのメモリの開放について

mishima at momo.so-net.ne.jp mishima at momo.so-net.ne.jp
Sun Dec 3 11:29:00 CST 2000


福原さん

今日(おっと、もう昨日ですね)の舟木さんちでの Age of Kings 対戦は
楽しかったですね。是非またやりましょう。(^ ^)

さて、車の中で話していた、配列を undef すると使用していたメモリは開放
されるか?という問題ですが、perlfaq の How can I free an array or hash
so my program shrinks? に書いてありました。読んでみてください。

確保したメモリをOSに返却してプロセスのサイズが減るわけではないですが、
Perl の中で再利用することはできるようです。(後半部分の抜粋↓)

    However, judicious use of my() on your variables will help make
    sure that they go out of scope so that Perl can free up their
    storage for use in other parts of your program. A global
    variable, of course, never goes out of scope, so you can't get
    its space automatically reclaimed, although undef()ing and/or
    delete()ing it will achieve the same effect. In general, memory
    allocation and de-allocation isn't something you can or should
    be worrying about much in Perl, but even this capability
    (preallocation of data types) is in the works.

確保したメモリは、my で定義した変数がスコープを終えたり、グローバル変
数を undef したり、ハッシュの要素を delete したりすると、メモリをPerl 
の中で再利用することはできる。しかし、メモリの確保や開放は、Perlではあ
んまり気にするものではない。

てなところでしょうか。


ついでに調べたのですが、配列 @a = (1,2,3); に対して、undef @a; すると
やっぱり @a は undef になり、@a = (); とすると0個の配列になりますね。
しかし、perldoc -f defined によると、その違いは将来なくなるようです。

    Use of `defined' on aggregates (hashes and arrays) is
    deprecated. It used to report whether memory for that aggregate
    has ever been allocated. This behavior may disappear in future
    versions of Perl. You should instead use a simple test for size:

        if (@an_array) { print "has array elements\n" }
        if (%a_hash)   { print "has hash members\n"   }

--
$p='Perl';$_='MISHIMA Masahiro';tr/A-Z/c-u/;tr/iron or steel/t/;
sub _ {pack'c*',$x=110+ at _*5,$x+1}@x=(sort(grep{!$_{$_}++}split//),
$p=~/(.(..).)/);$x[5]=~s/^/_/e;$x[6].=_ 1;for(@w=(47,1639,8,31259))
{s/\d/$x[$&-1]/g}print ucfirst "@w.\n";



More information about the Kansai-pm mailing list