Page 1 of 1
Finding memory leaks
Posted: Sun Sep 15, 2002 3:27 pm
by nielsene
I appear to have developed a memory leak in my recent script as evidenced by this error message
Code: Select all
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 36 bytes) in {snip}/CLASSES/CompDB.inc on line 1202
Does anyone know of a function that retuens the amout of memory used by PHP, so I can try to isolate where I'm loosing memory? There is no reason for my script to be using that much space...
Variables are automatically garbage-collected when they are no longer reachable, correct?
Posted: Mon Sep 16, 2002 5:10 am
by Takuma
Can we see the code?
Posted: Mon Sep 16, 2002 7:58 am
by nielsene
Sigh, it was a very stupid error on my part. A counter wasn't getting reset so a loop was repeating endlessly, tacking on an empty table cell to a string on every pass. The string got bigger than memory before the script execution time limit was hit.
I'm not use to memory bugs actually occuring where the program crashes, normally its due to something much earlier in the program.
But the source is at
http://ballroom.mit.edu/compinabox/Publ ... B.inc.phps
(forked version, doesn't include some cleanups suggested people people here on other code files at this point)
The line in question is the second while loop in the function rebuildCategories.
Posted: Mon Sep 16, 2002 9:06 am
by BDKR
That's not a memory leak in the classic sense. I guess it is in the sense that you don't know where memory is going. However, notice that the error says that you reached the limit of "Allowed" memory, which by the way can be adjusted upwards in the php.ini file. This is really a logic issue.
Was also thinking that perhaps it's possible to parse the output of something like free, top, or ps if you are using Linux. At that rate, it's may be possible to see what your script is doing. If there are any native functions that can do this, I don't know about them.
Anwyays, glad you got it sorted.
Cheers,
BDKR
Posted: Mon Sep 16, 2002 9:30 am
by nielsene
True its not a classic memory leak, but when a script goes from using a few hundred k to 8 MB its a little disturbing. And yes it was a logic error. Raising the allowed memory was my first thought, but of course it wouldn't have solved anything, except maybe changing the out of memory error to a time out error.
I was really wondering if there was some un-documented top/free-like built in, which it appears there isn't.
Posted: Mon Sep 16, 2002 10:00 am
by BDKR
Well, like I said, I'm glad you got it sorted.
Cheers,
BDKR
Posted: Mon Sep 16, 2002 3:47 pm
by llimllib
Imagine the posts we'd be getting in here if people had to deal with freeing their own memory

Posted: Mon Sep 16, 2002 4:56 pm
by BDKR
Yeah, we've got it easy with PHP.
Later on,
BDKR