gpt4 book ai didi

perl - Perl 中如何释放内存?

转载 作者:行者123 更新时间:2023-12-02 18:07:42 24 4
gpt4 key购买 nike

我的代码如下:

 my %var;
my %var_new={};

while(1){
while(my ($k,$v)=each %var){
&a_sub($v);
}
%var={}; # A
map { $var{$_}=$var_new{$_}; } keys %var_new;
%var_new={}; # B
}

sub a_sub { #....} # will fill %var_new

我的程序的内存使用量不断增加。

Perl 似乎没有在 A 行和 B 行释放内存。

如何手动让 Perl 释放使用 %var%var_new 的内存?

最佳答案

如果您使用了严格警告,您会看到:

Reference found where even-sized list expected

A 行和 B 行的功能与您想象的不同。他们实际上将空哈希引用的字符串化版本指定为键,并将 undef 作为其值。即使散列现在几乎为空,内存也不会被标记为可重用,因为您没有为此使用正确的语句。

尝试在 A 行和 B 行中使用以下语句之一:

undef %var; # this one
%var = (); # or this one

关于perl - Perl 中如何释放内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com