在 Perl 中逐行读取文件时是否应该积极释放内存?

发布于 2024-08-13 04:57:02 字数 175 浏览 3 评论 0原文

我应该在逐行读取文件时积极释放内存吗? 一个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-08-20 04:57:02

不。请参阅 perlfaq3 了解关于 Perl 中的内存使用应该(和不应该)做什么的更多信息。

No. See perlfaq3 for more on what you should (and shouldn't) do with regards to memory usage in Perl.

难理解 2024-08-20 04:57:02

这应该没有什么区别,因为无论您是否显式取消定义变量,变量都会在下一个循环迭代开始时被释放。

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.

苦行僧 2024-08-20 04:57:02

不会。除非文件中的行长度相差很大,并且其中一些长度非常巨大(比如说 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文