in reply to Weird memory leak in Catalyst application using Catalyst::Model::KiokuDB

You can try Test::LeakTrace. If there's any XS involved you may also want Valgrind

  • Comment on Re: Weird memory leak in Catalyst application using Catalyst::Model::KiokuDB

Replies are listed 'Best First'.
Re^2: Weird memory leak in Catalyst application using Catalyst::Model::KiokuDB
by Corion (Patriarch) on Aug 01, 2011 at 14:07 UTC

    How would Valgrind help with Perl data structures?

    As far as I'm aware, Valgrind can only check whether a C program overwrites memory it has not allocated, and maybe can check whether a C program allocates memory and then loses the pointer(s) to that piece of memory. If you have a valid Perl scalar, Perl will keep a valid (C) pointer to it, and will release the memory during Global Destruction. If that's true, then Valgrind will/can not flag this Perl value as ever lost, even though it may be a memory leak in the sense that there is no Perl structure from the running set of Perl variables to access that piece of memory.

      I recently spent quite a lot of time trying to find memory leak in Perl code, and finally ran it through Valgrind and discovered that it is YAML::XS. For sure it will not help with Perl structures, but I never said that.

Re^2: Weird memory leak in Catalyst application using Catalyst::Model::KiokuDB
by parmus (Novice) on Aug 02, 2011 at 14:31 UTC

    Test::LeakTrace might have put me on the right track, and it looks like I can narrow it further down to KiokuDB:

    use strict; use warnings; use Test::More; use Test::Exception; use Test::LeakTrace; use KiokuX::Model; my $model; eval { $model = KiokuX::Model->new(dsn => 'couchdb::uri=http://127.0.0 +.1:5984/lb-local'); }; plan skip_all => 'Cannot connect to CouchDB' if ($@); { no_leaks_ok { { my $scope = $model->new_scope; } } 'new_scope must not leak'; } done_testing;

    gives me:

    ot ok 1 - new_scope must not leak (leaks 2 <= 0) # Failed test 'new_scope must not leak (leaks 2 <= 0)' # at KiokuX_Model.t line 17. # '2' # <= # '0' # leaked SCALAR(0x4159810) from /usr/lib64/perl5/vendor_perl/5.12.4/Ki +okuDB/LiveObjects.pm line 191. # 190: # 191: $known->remove($scope); # 192: # SV = IV(0x4159808) at 0x4159810 # REFCNT = 1 # FLAGS = (IOK,pIOK) # IV = 0 # leaked ARRAY(0x4201180) from /usr/lib64/perl5/vendor_perl/5.12.4/Kio +kuDB/LiveObjects.pm line 260. # 259: # 260: $self->_known_scopes->insert($child); # 261: # SV = PVAV(0x4578968) at 0x4201180 # REFCNT = 2 # FLAGS = () # ARRAY = 0x4580b40 # FILL = 0 # MAX = 3 # ARYLEN = 0x0 # FLAGS = (REAL) # Elt No. 0 # SV = IV(0x4159808) at 0x4159810 # REFCNT = 1 # FLAGS = (IOK,pIOK) # IV = 0 1..1 # Looks like you failed 1 test of 1.

    Valgrind also complained about something in Set::Object, so it might be the source of the problem. I'll keep digging.

      I think I got my leak narrowed down now, and it looks like Set::Object::Weak is the problem. I've written the following short test script:

      use strict; use warnings; use Config; use Test::More; use Test::LeakTrace; use Set::Object; { package Foo; use Moose; 1; } { no strict; note join ' ', map {$Config{$_}} qw(osname archname); note 'perl version ', $]; note $_,'-',${"${_}::VERSION"} for qw{Moose Set::Object Test::Leak +Trace}; } my $set; { $set = Set::Object->new; no_leaks_ok { { my $obj = Foo->new; $set->insert($obj); $set->remove($obj); } } 'Testing Set::Object for leaking'; } { $set = Set::Object::Weak->new; no_leaks_ok { { my $obj = Foo->new; $set->insert($obj); $set->remove($obj); } } 'Testing Set::Object::Weak for leaking'; } done_testing;

      which gives me:

      # linux x86_64-linux # perl version 5.012004 # Moose-2.0202 # Set::Object-1.28 # Test::LeakTrace-0.13 ok 1 - Testing Set::Object for leaking (leaks 0 <= 0) not ok 2 - Testing Set::Object::Weak for leaking (leaks 2 <= 0) # Failed test 'Testing Set::Object::Weak for leaking (leaks 2 <= 0)' # at Set_Object.t line 39. # '2' # <= # '0' # leaked SCALAR(0xdad3a0) from Set_Object.t line 37. # 36: $set->insert($obj); # 37: $set->remove($obj); # 38: } # SV = IV(0xdad398) at 0xdad3a0 # REFCNT = 1 # FLAGS = (IOK,pIOK) # IV = 0 # leaked ARRAY(0x1163cd0) from Set_Object.t line 36. # 35: my $obj = Foo->new; # 36: $set->insert($obj); # 37: $set->remove($obj); # SV = PVAV(0x1394b00) at 0x1163cd0 # REFCNT = 2 # FLAGS = () # ARRAY = 0x13ac740 # FILL = 0 # MAX = 3 # ARYLEN = 0x0 # FLAGS = (REAL) # Elt No. 0 # SV = IV(0xdad398) at 0xdad3a0 # REFCNT = 1 # FLAGS = (IOK,pIOK) # IV = 0 1..2 # Looks like you failed 1 test of 2.

      Can anyone else here verify this? And if not, could I kindly ask you to post the top 5 lines of the output (the os, arch and versions)?