in reply to memory leak

Will the array of 100 objects and hash in the sub GetObjs be reclaimed/re-used or will another chunk of memory for 100 objects be taken every call to this sub?

Yes. Once there are no longer any references to a variable ( as would happen to @objects, $object, %ht ) they will be destroyed. This won't give the memory back to the OS, but perl will re-use it internally.

If you aren't using them, you should be using 'strict' and probably 'warnings' because it may point out places where you are accidentally using a global variable where you didn't mean to.

The code you posted doesn't run out of memory for me.

-- AlexLC

Replies are listed 'Best First'.
Re^2: memory leak
by bob_dobalina (Sexton) on Aug 26, 2009 at 03:31 UTC
    thanks for the reply.
    i am using strict and warnings.
    i really try to avoid globals; this example is the only place i could see really eating up memory. how about this:
    assuming the same code, say you took each object and passed it around:
    while($i < 20000000) { @objects = getObjects(); foreach my $obj (@objects) { SomeMethod($obj); } $i += 100; } sub SomeMethod { my $obj = shift; AnotherMethod($obj); } sub AnotherMethod { my $obj = shift; }
    Isn't the $obj param essentialy a hash ref? This doesn't increment the reference count in every sub does it? It should all be one reference to the same memory location, right?

    Or - what if the object had a "member" that was a ref to an array of other objects, could that cause problems?:
    -------------------------------- package Obj; sub new { my $class = shift; my $name = shift; my $arrayRef = shift; my $self = { NAME => $obj, ARRAY_REF => $arrayRef }; bless $self, $class; return $self; } return 1; # ex: my $obj = new Obj("x", \@arrayOfObjects);