Finding 'lost' variables (circular references)
Asked Answered
C

1

8

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.

Cankered answered 15/8, 2015 at 12:24 Comment(5)
I know that search.cpan.org/~lds/Devel-Cycle-1.10/lib/Devel/Cycle.pm and search.cpan.org/~ilyaz/Devel-Peek-0.96/Peek.pm both help with these issues, but I've never used them when the vars fall out of scope and get 'lost', so I don't know if they're useful in those cases. Worth a look thoughDermatoid
Maybe Devel::LeakTrace::Fast?Windermere
metacpan.org/pod/Devel::Gladiator, metacpan.org/pod/Devel::MAT::Dumper, pmat-explore-gtkKosak
I don't see how Devel::Peek would help, but this is what Devel::Cycle is meant forYolandayolande
If you wanted to check a specific class for leaks, you could give it a destructor, and see if it's called when you expect it to be.Yolandayolande
T
7

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.

Tonkin answered 15/8, 2015 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.