常见的Perl内存/参考泄漏模式?

编程入门 行业动态 更新时间:2024-10-28 03:25:36
本文介绍了常见的Perl内存/参考泄漏模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在追逐Perl代码库中的一些潜在的内存泄漏,并且我想了解有关Perl中的内存(错误)管理的常见陷阱.

I'm chasing a couple of potential memory leaks in a Perl code base and I'd like to know about common pitfalls with regards to memory (mis-)management in Perl.

您在Perl代码中观察到哪些常见的泄漏模式?

What are common leak patterns you have observed in Perl code?

推荐答案

循环引用是迄今为止最常见的导致泄漏的原因.

Circular references are by far the most commonthe canonical cause of leaks.

sub leak { my ($foo, $bar); $foo = \$bar; $bar = \$foo; }

Perl使用引用计数垃圾收集.这意味着perl会统计给定时间存在任何指向任何变量的指针.如果变量超出范围并且计数为0,则清除该变量.

Perl uses reference counting garbage collection. This means that perl keeps a count of what pointers to any variable exist at a given time. If the variable goes out of scope and the count is 0, the variable is cleared.

在上面的示例代码中,从不收集$foo和$bar,并且每次调用leak()后,副本都会持久存在,因为两个变量的引用计数均为1.

In the example code above, $foo and $bar are never collected and a copy will persist after every invocation of leak() because both variables have a reference count of 1.

防止此问题的最简单方法是使用弱引用.弱引用是访问数据时遵循的引用,但不计入垃圾回收.

The easiest way to prevent this issue is to use weak references. Weak references are references that you follow to access data, but do not count for garbage collection.

use Scalar::Util qw(weaken); sub dont_leak { my ($foo, $bar); $foo = \$bar; $bar = \$foo; weaken $bar; }

在dont_leak()中,$foo的引用计数为0,$bar的引用计数为1.当我们离开子例程的范围时,$foo返回到池中,并且其引用为$bar被清除.这样会将$bar上的引用计数降低为0,这意味着$bar也可以返回到池中.

In dont_leak(), $foo has a reference count of 0, $bar has a ref count of 1. When we leave the scope of the subroutine, $foo is returned to the pool, and its reference to $bar is cleared. This drops the ref count on $bar to 0, which means that $bar can also return to the pool.

更新: Brain d Foy询问我是否有任何数据来支持我的论点,即循环引用是常见的.不,我没有任何统计数据可证明循环引用是常见的.它们是Perl内存泄漏最常被谈论和记录最好的形式.

Update: brain d foy asked if I have any data to back up my assertion that circular references are common. No, I don't have any statistics to show that circular references are common. They are the most commonly talked about and best documented form of perl memory leaks.

我的经验是它们确实发生了.这是我十年来与Perl一起工作时看到的内存泄漏的简要介绍.

My experience is that they do happen. Here's a quick rundown on the memory leaks I have seen over a decade of working with Perl.

我在pTk应用开发漏洞时遇到了问题.我能够证明的一些泄漏是由于Tk绕过窗口参考时出现的循环参考而引起的.我也看到过pTk泄漏,其原因我无法追查.

I've had problems with pTk apps developing leaks. Some leaks I was able to prove were due to circular references that cropped up when Tk passes window references around. I've also seen pTk leaks whose cause I could never track down.

我已经看到人们误解了weaken并偶然发现了循环引用.

I've seen the people misunderstand weaken and wind up with circular references by accident.

我看到无意识的循环会突然出现,因为有太多糟糕的想法被匆忙地扔到了一起.

I've seen unintentional cycles crop up when too many poorly thought out objects get thrown together in a hurry.

有一次我发现内存泄漏来自XS模块,该模块正在创建大型的深层数据结构.我从来没有得到过比整个程序都要小的可复制测试用例.但是,当我用另一个串行器替换模块时,泄漏消失了.所以我知道那些泄漏来自XS.

On one occasion I found memory leaks that came from an XS module that was creating large, deep data structures. I was never able to get a reproducible test case that was smaller than the whole program. But when I replaced the module with another serializer, the leaks went away. So I know those leaks came from the XS.

因此,根据我的经验,周期是泄漏的主要来源.

So, in my experience cycles are a major source of leaks.

幸运的是,有一个模块可以帮助您追踪他们.

Fortunately, there is a module to help track them down.

关于从未被清理的大型全球结构是否构成泄漏",我同意布赖恩的观点.它们像泄漏一样嘎嘎作响(由于错误,我们的进程内存使用量一直在增长),因此它们就是泄漏.即使这样,我也不记得曾经在野外看到过这个特殊问题.

As to whether big global structures that never get cleaned up constitute "leaks", I agree with brian. They quack like leaks (we have ever-growing process memory usage due to a bug), so they are leaks. Even so, I don't recall ever seeing this particular problem in the wild.

根据我在巨石阵网站上看到的内容,我猜布莱恩从他正在接受培训或为之创造治疗奇迹的人们身上看到了很多病态的代码.因此,他的样本集比我的样本集要容易得多,而且种类繁多,但它有其自身的选择偏差.

Based on what I see on Stonehenge's site, I guess brian sees a lot of sick code from people he is training or preforming curative miracles for. So his sample set is easily much bigger and varied than mine, but it has its own selection bias.

哪种原因最常见?我认为我们永远不会真正知道.但是我们都可以同意,循环引用和全局数据垃圾场是反模式,需要在可能的情况下予以消除,并且在有意义的少数情况下应谨慎处理.

Which cause of leaks is most common? I don't think we'll ever really know. But we can all agree that circular references and global data junkyards are anti-patterns that need to be eliminated where possible, and handled with care and caution in the few cases where they make sense.

更多推荐

常见的Perl内存/参考泄漏模式?

本文发布于:2023-11-13 14:06:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   常见   模式   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!