查找“丢失"的变量(循环引用) [英] Finding 'lost' variables (circular references)

查看:51
本文介绍了查找“丢失"的变量(循环引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是一个有点简单的案例 - 但如果我像这样设置循环引用:

#!/usr/bin/perl使用严格;使用警告;{我的$东西;我的$otherthing;$东西 ->{otherthing} = \$otherthing;$otherthing ->{东西} = \$东西;}

我造成了内存泄漏 - 因为通过引用计数,这里分配的内存永远不会被释放,尽管没有任何向外的访问点".

所以我想知道 - 在这种情况下,有什么方法可以 - 通过调试或类似方式 - 重新发现"这些变量并再次访问它们?

假设我在考虑一个不那么琐碎的情况——你有一个内存泄漏的对象,但想再次捕捉"它以查看其中的内容,以提示所述对象中的内容,因此问题开始的地方.

解决方案

这只是对问题评论中推荐的模块的精选.它包括每个模块的 POD 文档的链接,并引用名称描述部分.它意味着可以访问.我没有添加或更改作者写的内容,也没有从名称和描述部分以外的任何地方提取信息

欢迎任何人使用其他模块对其进行更新,只要他们保持这种格式即可.或者,任何人都可以让我注意它需要更新的事实,我会在有能力的时候这样做

许多优秀的程序员都不太擅长英语,所以虽然我试图准确地引用文档,但为了与本文的目的保持一致,我已经删除了我认为不太相关的部分

<块引用>

Devel::Cycle - 在对象中查找内存周期

这是一个简单的开发人员工具,用于在对象和其他类型的引用中查找循环引用.由于 Perl 基于引用计数的内存管理,循环引用会导致内存泄漏.

<块引用>

Devel::LeakTrace::Fast- 指出泄漏变量的来源.

Devel::LeakTrace::Fast 是对 Devel::LeakTrace 的重写.与 Devel::LeakTrace 一样,它使用 perl 5.6 及更高版本中的可插入 runops 功能来跟踪正在运行的程序的 SV 分配.

END 时间 Devel::LeakTrace::Fast 识别任何剩余的变量,并报告出现的行.

<块引用>

Devel::Gladiator - 走在 Perl 的舞台上>

Devel::Gladiator 迭代 Perl 的内部内存结构,可用于枚举所有当前活动的 SV.

这可用于寻找泄漏和分析内存使用情况.

<块引用>

Devel::MAT::Dumper- 写一个堆转储文件供以后分析

此模块提供内存转储功能,该功能创建一个堆转储文件,稍后可以由 Devel::MAT::Dumpfile.它提供了一个不导出的函数,它将文件写入给定的路径.

转储文件将包含 Perl 领域中每个 SV 的表示,提供有关它们之间的指针的信息,以及有关进程创建时的状态的其他信息.它包含当时过程的快照,稍后可以通过各种工具使用 Devel::MAT::Dumpfile 加载和分析.

<块引用>

Devel::Peek - 一个数据调试工具XS程序员

Devel::Peek 包含允许从 Perl 脚本操作原始 Perl 数据类型的函数.那些进行 XS 编程的人使用它来检查他们从 C 发送到 Perl 的数据是否看起来像他们认为应该看起来的那样.

So, it's a bit of a simplistic case - but if I set up a circular reference like this:

#!/usr/bin/perl
use strict;
use warnings;

{ 
   my $thing; 
   my $otherthing;
   $thing -> {otherthing} = \$otherthing;
   $otherthing -> {thing} = \$thing; 
}

I create a memory leak - because by reference counting, the memory allocated here will never be freed, despite not having any outward 'access point'.

So what I'm wondering - in this sort of scenario, is there any way I could - via debugging or similar - 'rediscover' these variables and gain access to them again?

Hypothetically I'm thinking in a less trivial case - you've a memory leaked object, but would like to 'catch' it again to see what's in it, to give a hint as to what was in said object and thus where the problem was starting.

解决方案

This is just a curation of the modules recommended in the comments on the question. It includes a link to each module's POD documentation, and quotes the name and description sections. It is meant to be accessible. Nowhere have I added to or changed what the author wrote, or pulled information from anywhere other than the name and description sections

Anyone is welcome to update it with additional modules as long as they keep to this format. Alternatively, anyone may draw my attention to the fact that it needs to be updated and I will do so when I am able

Many great programmers are not so great with English so, while I have tried to quote the documentation exactly, I have cut what I believe to be less relevant sections in keeping with the purpose of this post

Devel::Cycle - Find memory cycles in objects

This is a simple developer's tool for finding circular references in objects and other types of references. Because of Perl's reference-count based memory management, circular references will cause memory leaks.

Devel::LeakTrace::Fast - Indicate where leaked variables are coming from.

Devel::LeakTrace::Fast is a rewrite of Devel::LeakTrace. Like Devel::LeakTrace it uses the pluggable runops feature found in perl 5.6 and later in order to trace SV allocations of a running program.

At END time Devel::LeakTrace::Fast identifies any remaining variables, and reports on the lines in which the came into existence.

Devel::Gladiator - Walk Perl's arena

Devel::Gladiator iterates Perl's internal memory structures and can be used to enumerate all the currently live SVs.

This can be used to hunt leaks and to profile memory usage.

Devel::MAT::Dumper - Write a heap dump file for later analysis

This module provides the memory-dumping function that creates a heap dump file which can later be read by Devel::MAT::Dumpfile. It provides a single function which is not exported, which writes a file to the given path.

The dump file will contain a representation of every SV in Perl's arena, providing information about pointers between them, as well as other information about the state of the process at the time it was created. It contains a snapshot of the process at that moment in time, which can later be loaded and analysed by various tools using Devel::MAT::Dumpfile.

Devel::Peek - A data debugging tool for the XS programmer

Devel::Peek contains functions which allows raw Perl datatypes to be manipulated from a Perl script. This is used by those who do XS programming to check that the data they are sending from C to Perl looks as they think it should look.

这篇关于查找“丢失"的变量(循环引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆