good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Mod_perl, Memory Leaks and WeakRefby djantzen (Priest) |
on Dec 25, 2003 at 00:22 UTC ( [id://316940]=perlquestion: print w/replies, xml ) | Need Help?? |
djantzen has asked for the wisdom of the Perl Monks concerning the following question: Hi All, Well it looks like jolly St. Nick brought me a memory leak this year. It was something I had a good idea was coming and thought I had a handle on, but apparently not. What I've got is a set of modules serving as the backend for a CGI::Application-based script running under mod_perl. The trouble is that the modules create datastructures with lots of circular references. Essentially I've got a directed graph implemented as an adjacency list so edges and vertices refer to one another. Furthermore, each vertex in the graph contains a tree where parent and child nodes each hold a reference to one another as a doubly-linked list. My plan was to use WeakRef to break these circular references, and it seems to work in cases such as my global object cache and in the tree nodes. However, in other cases where the "forward" and "back" references are not so clear, i.e., in my graph edges and vertices, I've run into premature garbage collection leading to some extremely vexing "can't call methodX on an undefined value" errors. So I've removed the trouble making weakenings, but now it looks a lot like I've got a memory leak under mod_perl. My tools for figuring this are admittedly crude, looking at the output from 'top' and watching the memory addresses increase on each invocation of the script. I'm trying to use Apache::Leak as well (calling it in my CGI like leak_test { $my_app->run() };) but it dumps core before providing any useful information: "FILE = 'Wed Dec 24 19:07:45 2003 notice child pid 24391 exit signal Segmentation Fault (11), possible coredump'" Now I'm very new to Web programming, so perhaps I'm missing an obvious answer, but I need some help solving this. So far I've tried manually calling DESTROY on all my objects in my implementation of CGI::Application::teardown, which, to my surprise, appears not to work. I thought for sure the only drawback was the ugliness of brute-forcing the solution, but apparently I'm missing something about perl's GC. I'm wondering now if I need to traverse my datastructures and manually break those circular references. This would have added benefit since further usages of these modules will face the same problem in some imaginable contexts, so writing code to perform it reliably could come in handy down the road. Or is there a way to tell mod_perl to blow away all object references in a process when the CGI request ends without requiring recompilation of the files on every invocation? Thanks much, David
"The dead do not recognize context" -- Kai, Lexx
Back to
Seekers of Perl Wisdom
|
|