halfcountplus has asked for the wisdom of the Perl Monks concerning the following question:
I've written an http server, much of it using Inline::C, and I have a memory leak I cannot pin down.
Every few score requests, mem usage grows by exactly 132 kB. By "every few score" I mean irregularly, and on average. Sometimes it happens after 10 requests, sometimes 100. The requests are all identical, generated via apachebench.
I do not believe this is because of anything explicit in the C routines since there is only one malloc call, for a file slurp, which in this test would only be a few hundred bytes, the pointer is free'd a few lines later after its content is assigned to an SV, and such a leak would grow in a regular linear fashion every time a new memory page was needed.
132 kB is a power of two value so I suspect it is some data structure perl is growing. However, I cannot find anything leaky in my perl code either -- I have checked for circularity with Test::Memory::Cycle, and I have a recursive rountine for checking reference counts of objects/structures and their members before they are deleted or undef'd.
I've tried valgrind, but it is almost useless for me with perl since even "helloworld.pl" reports thousands of kB remaining in hundreds of blocks.
Anyone know a way I can trace allocation, hopefully in real time?
later: I did find one stray reference that accounts for the major leak, however I still have the same issue on a much smaller scale -- the increase is now every 1000-2000 requests. See below http://www.perlmonks.org/?node_id=925997 for how I am tracing references.
finally: I found the smaller leak by watching PL_sv_count as per Dave's suggestion, qv. http://www.perlmonks.org/?node_id=926128. No more leaky http server!
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tracing memory leak
by BrowserUk (Patriarch) on Sep 14, 2011 at 18:09 UTC | |
by halfcountplus (Hermit) on Sep 14, 2011 at 19:18 UTC | |
by dave_the_m (Monsignor) on Sep 14, 2011 at 20:04 UTC | |
by halfcountplus (Hermit) on Sep 14, 2011 at 20:43 UTC | |
by dave_the_m (Monsignor) on Sep 14, 2011 at 22:11 UTC | |
| |
by BrowserUk (Patriarch) on Sep 14, 2011 at 19:43 UTC | |
by halfcountplus (Hermit) on Sep 14, 2011 at 20:16 UTC | |
by BrowserUk (Patriarch) on Sep 15, 2011 at 00:56 UTC | |
| |
|
Re: Tracing memory leak
by Anonymous Monk on Sep 14, 2011 at 18:08 UTC | |
by halfcountplus (Hermit) on Sep 14, 2011 at 18:55 UTC | |
|
Re: Tracing memory leak
by halfcountplus (Hermit) on Sep 14, 2011 at 20:31 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |