Finding memory leaks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Finding memory leaks

Post 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?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Can we see the code?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Well, like I said, I'm glad you got it sorted.

Cheers,
BDKR
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Imagine the posts we'd be getting in here if people had to deal with freeing their own memory :)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Yeah, we've got it easy with PHP.

Later on,
BDKR
Post Reply