I don't have any experience with Win32::ODBC, but looking at the release notes, $db shouldn't be the problem, as it is only storing the connection, not the data, and it falls out of scope at the end of the scope anyway, so it should be garbage collected and returned to the pool anyway.

Are you sure that $db is being closed every time? Your script will die if the connection is not made, so there is no need for the if ($db){...} loop.

I would also get rid of the global variable @objects and put the definition inside the while($i < 20000000) loop. This way you know that the array and it's contents are definitely out of scope with each iteration of the while loop.

Perl garbage collection is refernce count based, (as i am sure everyone has already said), so if your memory really does continue to grow even though you have tightened the scoping, and are sure your new Obj("name_$i", $i) has not circularities, then my suspiscion would be that the ODBC memory is not being freed to the memory pool allocated to perl, but even this shouldn't be an issue because you are not making new DBs or adding to the DB right?

One last thought, just to make sure your objects are being destroyed, you could add some debugging code along the lines of :

use Win32::ODBC; use warnings; use strict; my $i = 0; while($i < 20000000) { print "***NEW ITERATION \$i = $i\n"; my @objects = getObjects(); # do something $i += 100; } sub getObjects { my @objects = (); my $object = undef; my $db = undef; my $i = 0; $db = new Win32::ODBC("DSN=dsnname;UID=username;PWD=pw%") || die " +Failed to establish connection\n"; for($i=0; $i<100; $i++) { #$object = new Obj("name_$i", $i); $objects[$i] = $object; print "Object ".( $object->to_string() )." created.\n"; } $db->Close(); return \@objects; } ################# IN Obj class definition ########### sub DESTROY{ my $self = shift; print "".( $self->to_string() )." biting the dust.\n"; }

This way you should be able to be really sure that all Obj objects are being destroyed each iteration.

Hope this helps, but i am not sure! Good luck!

Just a something something...

In reply to Re^5: memory leak by BioLion
in thread memory leak by bob_dobalina

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.