Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Bless MyObject

by perlcapt (Pilgrim)
on Oct 22, 2004 at 19:44 UTC ( #401655=perlquestion: print w/replies, xml ) Need Help??
perlcapt has asked for the wisdom of the Perl Monks concerning the following question:

Without going into the details of my cruddy code, let me outline the situation and then state the problem.

The Situation

I did a proof of concept, procedural hack that:
  1. reads chunks of data in from a binary file using sysread
  2. finds the overal structural separators that define data packets
  3. determines what kind of data packet it is
  4. further analyses the packet, breaking it into its separate channels
Now the project becomes real, requiring more sophistication, I take the "proof of concept" procedural hack and develop it into a perl module.

The main class is an data analyzer. The calling program, creates a new instansiation of this class, once, at the begining. After calling a method to open a data file, it then calls a method for giving it the next packet. The GetNextPacket method creates a new data object (using another class) for each data packet found. GetNexPacket returns the new object to the calling program, which typically keeps only a few of this packets around for data splicing.

The Problem

It appears, because of deteriorating performance, that these data objects are not being clobbered when they are no longer referenced by the calling program. (In fact, in the testing program for the module, the returned value of GetNextPacket is simply assigned to a scalar that is overwritten on each call.)

The Question(s)

How can I determined if these data objects are more persistant than I want? (I have looked at the process with both top (Linux) and Komodo (Dimdows), and nothing seems to be growing.) When should I create (bless) a new object, when not?



Replies are listed 'Best First'.
Re: Bless MyObject
by Joost (Canon) on Oct 22, 2004 at 20:09 UTC
    As long as your objects are not referring to each other or creating circular references in other ways, they should be removed when they go out of scope (technically, when their reference count goes to 0).

    If you want notices of your objects being destroyed, you can create a DESTROY method in their class:

    package MyClass; sub DESTROY { my $self = shift; # assuming object has a "name" property warn "object $self->{name} is being destroyed now"; }

    Depending on your problem, creation and destruction of your objects might be a bottleneck (I've hardly ever been in that situation, but still). If so, you could consider "reusing" the same object, or using class methods (if you're only using one instantiation at a time anyway), or getting rid of the object altogether and using hash references or scalars, or array refs instead.

    Give Devel::DProf a shot, before you start optimizing. It will probably give you more insight in what part of your code is causing the problem.

Re: Bless MyObject
by BrowserUk (Pope) on Oct 22, 2004 at 20:11 UTC

    Add a DESTROY method to your class(es). You shouldn't need to do anything, but us it to log when thing are being GC'd. Add some logging to your new() method to log what and when things are being created. Cycle the code. That should allow you to track whether things are persisting longer than you think they should.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://401655]
Approved by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-02-20 00:17 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.