tall_man has asked for the wisdom of the Perl Monks concerning the following question:

I have a large perl/Tk program which uses many modules, and I am trying to trace where it's spending its time after I destroy the main window. It takes several seconds for the program to end, and during that time the CPU is maxed out. I tried Devel::AutoProfiler and it failed with "deep recursion."

I put a simple trace into the DESTROY method of Tk::Widget.pm:

sub DESTROY { my $w = shift; print ref($w) . " destroy called\n"; $w->destroy if ($w->IsWidget); print ref($w) . " destroy done\n"; }
It looks like the slowdown takes place after all the widgets are destroyed, including MainWindow. Possibly it's happening in the global destroy phase.

Is there any way to trace what is happening in the clean-up phase, or does anyone have suggestions on where Windows might be spending so much time? The same program ends quickly under Linux.

Replies are listed 'Best First'.
Re: Slow wind-up in Windows?
by BrowserUk (Patriarch) on Jun 17, 2003 at 22:34 UTC

    On the basis on very little other than instinct, this sounds like it could be related to the same problem of freeing up hash buckets as discussed and described here freeing hashes on Linux?

    The difference between the windows and Linux could be down to the version of malloc used by the two builds of perl?

    It might be worth trying the POSIX::_exit() trick described in the thread above and see if you get any benefit.

    Just a thought.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      I tried the POSIX::_exit() and it does finish much faster. It looks like not all destructors are called, but that may be all right for this application.

        Makes sense. Bottling out without cleaning up may seem harsh, but provided you ensure that you have saved anything that needs to be saved, flushed any buffers and closed any open files yourself, waiting for the GC to free off all those little bits of memory, just so that you can exit clean is pretty redundant.

        If this is your own build of perl, then you could try switching to a different malloc(), if its an AS pre-built, maybe whatever malloc they use will change or improve next time around.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Slow wind-up in Windows?
by dash2 (Hermit) on Jun 18, 2003 at 09:46 UTC
    "Deep recursion" is a warning according to perldiag, so maybe you could profile your script with warnings turned off?

    andramoiennepemousapolutropon

      It doesn't help, because soon after that Windows runs out of virtual memory.
        Heh... I just thought it was the profiler recursing a lot, but maybe you really do have an infinite loop there when the profiler is turned on.

        The other thing you could do is try and debug it in the debugger, figure out what is going on that way. You might just spot something obvious. But I have to say, I don't have much experience with debugging global destruction. perldebug says:

        Set the "inhibit_exit" option to 0 if you want to be able to step off the end the script. You may also need to set $finished to 0 if you want to step through global destruction.

        It sounds to me like the posix::_exit() workaround is good enough.

        andramoiennepemousapolutropon