Skip to content

Commit

Permalink
Fix potential memory leak in stats server 3/3
Browse files Browse the repository at this point in the history
  • Loading branch information
iliajie committed Aug 2, 2024
1 parent 6fdd7e9 commit 7293ee5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
31 changes: 27 additions & 4 deletions stats-lib-funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ sub jsonify

sub get_stats_empty
{
my $time = time();
my $time = time();
return {
graphs =>
{ cpu => [{x => $time, y => 0}],
mem => [{x => $time, y => 0}],
{ cpu => [{x => $time, y => 0}],
mem => [{x => $time, y => 0}],
virt => [{x => $time, y => 0}],
proc => [{x => $time, y => 0}],
disk => [{x => $time, y => [0, 0]}],
net => [{x => $time, y => [0, 0]}]
net => [{x => $time, y => [0, 0]}]
}
};
}
Expand All @@ -84,6 +84,8 @@ sub get_stats_now
my ($k, $d) = @_;
$graphs->{$k} = [] if (ref($graphs->{$k}) ne 'ARRAY');
push(@{$graphs->{$k}}, {x => $time, y => $d});
# Release memory
undef($d);
};
# Collect stats
if ($foreign_check_proc) {
Expand All @@ -103,6 +105,9 @@ sub get_stats_now
$data{'io'} = [$in, $out];
$gadd->('disk', [$in, $out]);
}
# Release memory
undef(@cpuinfo);
undef(@cpuusage);
}
}
# Memory stats
Expand Down Expand Up @@ -130,6 +135,8 @@ sub get_stats_now
)];
$gadd->('virt', $virt);
}
# Release memory
undef(@memory);
}
}
# Number of running processes
Expand All @@ -138,6 +145,8 @@ sub get_stats_now
my $proc = scalar(@processes);
$data{'proc'} = $proc;
$gadd->('proc', $proc);
# Release memory
undef(@processes);
}
}
# Network I/O
Expand All @@ -151,6 +160,11 @@ sub get_stats_now
$data{'net'} = [$in, $out];
$gadd->('net', [$in, $out]);
}
# Release memory
undef($network);
undef($nrs);
undef($in);
undef($out);
}
# Reverse output for LTR users
if (get_text_ltr()) {
Expand All @@ -161,7 +175,10 @@ sub get_stats_now
}
}
}
# Assign data
$data{'graphs'} = $graphs;
# Release memory
undef($graphs);
# Return data
return \%data;
}
Expand Down Expand Up @@ -240,6 +257,8 @@ sub merge_stats {
$graphs1->{$key} = $graphs2->{$key};
}
}
# Release memory
undef($graphs2);
return $graphs1;
}

Expand All @@ -259,6 +278,10 @@ sub save_stats_history
lock_file($file);
write_file_contents($file, $json->encode($graphs));
unlock_file($file);
# Release memory
undef($graphs_chunk);
undef($all_stats_histoy);
undef($graphs);
}

1;
7 changes: 6 additions & 1 deletion stats.pl
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@
# Save stats to history and reset cache
if ($serv->{'ticked'}++ % 20 == 0) {
save_stats_history($stats_period);
$stats_period = undef;
undef($stats_period);
}
# If interval is set then sleep minus one
# second becase tick_period is one second
if ($serv->{'interval'} > 1) {
sleep($serv->{'interval'}-1);
}
# Release memory
undef($stats_now);
undef($stats_now_graphs);
undef($stats_now_json);
undef($stats_history);
},
on_connect => sub {
my ($serv, $conn) = @_;
Expand Down

0 comments on commit 7293ee5

Please sign in to comment.