Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding 'lost' variables (circular references)

Tags:

reference

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.

like image 213
Sobrique Avatar asked Aug 15 '15 12:08

Sobrique


1 Answers

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.

like image 115
Borodin Avatar answered Nov 11 '22 20:11

Borodin