On Fri, 6 Oct 2000, Andreas Schiffler wrote:

> Any ideas where this memory consumption might come from? or how to find
> the culprit through some mod_perl code?

Go buy some coffee, get a copy of your local pizza shop's menu next to
your desk, and bring in a sleeping bag. That should help you get through
it.

The problem is that of all the methods people told me how to use, it
became clear that none work. The problem is that memory allocation doesn't
happen where the leak is. It happens in pools. I don't know if this is the
way it works just on Linux, but it sure made the leaks hard to find.

The way I finally found the leaks in AxKit was two-fold:

The first way was to simply create an entirely new module, and copy code
from the old one into the new one bit by bit until the new module starts
leaking too. Use ab (apachebench - in your Apache bin directory) to run
lots of hits on the URL, or just plain-old lwp-request. When you add the
bit of code that leaks, you've found a leak. (yes, this sounds like a
nightmare, and it is, but it was the only thing that worked).

The second thing was to make really good use of exception handlers. The
way I now structure my code is with Error.pm:

use Apache::Constants;
use Error qw/:try/;

sub handler {
        my $r = Apache::Request->new(shift);

        return try {
                ... code here.
                return OK;
        }
        catch MyError::Declined with {
                return DECLINED;
        }
        catch MyError::OK with {
                return OK;
        }
        catch Error with {
                my $E = shift;
                die "handler died: $E\n";
        }
}

(this is only a tiny fragment of the code, obviously)

Now what you can do is add a line:

throw MyError::Declined ( -text => "Find that leak!" );

somewhere in your code. Now start moving that line deeper and deeper into
your code, until the leak starts occuring. Then you know you've moved it
one line past the leak. You can then step into the code, if its a function
call, and add that line there.

Oh, and you need to define MyError::* somewhere. Thats all documented in
the Error.pm docs.

Sounds like a long winded process? Sure. But I'm saving you a lot of
heartache looking for this. And you'll be glad of structured exception
handling in the long run.

(Stas - maybe I should clean this up and add it to the guide?)

-- 
<Matt/>

** Director and CTO **
**  AxKit.com Ltd   **  ** XML Application Serving **
** http://axkit.org **  ** XSLT, XPathScript, XSP  **
**     Personal Web Site: http://sergeant.org/     **

Reply via email to