Skip to content

Instantly share code, notes, and snippets.

@nihen
Last active December 26, 2015 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nihen/7085103 to your computer and use it in GitHub Desktop.
Save nihen/7085103 to your computer and use it in GitHub Desktop.
perl で do & forkするとcowにのってるメモリがexit時に再消費?
free -s 0.2 | grep Mem
を実行して眺めつつこのperl scriptを実行すると、子プロセスがexitする時に新たなメモリを消費するのがわかる。
CoWのメモリ分ではないかと思われる。
キモは do と fork の組み合わせ
perl 5.14.4, 5.18.1で確認
↓doは関係なかった
http://nihen.hatenablog.com/entry/2013/10/22/125410
use 5.14.4;
use File::Temp qw/tempfile/;
# { 1 => 'aiueo', 2 => 'aiueo',,,, }
my ($fh, $filename) = tempfile(UNLINK => 1);
print {$fh} qq|{\n|;
for ( 1..300000 ) {
print {$fh} qq|'$_' => 'aiueo',|, "\n";
}
print {$fh} qq|}\n|;
close $fh;
my $data = do $filename;
warn "$$ complete data load";
my %child_pid = ();
for ( 1..10 ) {
if ( my $pid = fork ) {
$child_pid{$pid} = 1;
}
else {
warn "$$ wake";
sleep 5;
warn "exit $$";
exit;
}
}
{
# wait_all_children
while ( %child_pid ) {
if ( my $pid = wait ) {
delete $child_pid{$pid};
}
}
}
warn "$$ parent exit";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment