在 Perl 中逐行读取文件时是否应该积极释放内存?
我应该在逐行读取文件时积极释放内存吗? 一个例子:
while (<FILE>) {
my $line = $_;
<process line>
undef($line);
}
“取消定义”$line 变量是减少内存消耗的好选择?
Should I aggressively release memory while reading a file, line by line?
An example:
while (<FILE>) {
my $line = $_;
<process line>
undef($line);
}
"Undefing" the $line variable is a good option for reducing memory consumption?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。请参阅 perlfaq3 了解关于 Perl 中的内存使用应该(和不应该)做什么的更多信息。
No. See perlfaq3 for more on what you should (and shouldn't) do with regards to memory usage in Perl.
这应该没有什么区别,因为无论您是否显式取消定义变量,变量都会在下一个循环迭代开始时被释放。
That should make no difference as the variable will be released as soon as the next loop iteration starts, whether you explicitly undef it or not.
不会。除非文件中的行长度相差很大,并且其中一些长度非常巨大(比如说 10 兆字节或以上),否则为什么还要担心呢?
是否有证据表明循环消耗了太多内存?
No. Unless the lines in your file are of wildly different lengths, and some of those lengths are enormous (10 megabytes and up let's say), why even worry about it?
Is there evidence that loop is consuming too much memory?