0

This seems to have been discussed quite a bit. I've tried several things I've found, but no luck.

I have a "hacky" cache built that stores objects received from XML calls. The XML calls proved to take too long on page loads, so a user can push a button in the admin to rebuild the cache. The cache routine keeps dying with a memory allocation error.

I know this is a very vague question, but I'm not sure it would help to post a big section of code either.

I am "unsetting" every varaible after I finish using it. I'm calling gc_collect_cycles() which doesn't seem to do anything at all.

I have two anonymous functions that get called over an over....could these be the culprits?

What should I be looking for? Would doing a sleep() help at all?

Code Edit

Here is the code: http://pastebin.com/8M1Dk73E

On line 79 of the pastebin code, I'm calling gc_collect_cycles. Not sure if that is a good place to put it or not.

I am using foreach loops instead of for loops, which I know makes a huge difference with objects being copied, but I would think if I unset the variables it should work the same even if the execution time is longer.

Well, I'm at a loss, so any thoughts would be helpful.

2
  • Several questions: How much memory is allocated to PHP? Could you print out the error message? If you run it ten times, while changing the order that items are being fetched, does it generally expire on the same line number (a sign that this might be the code responsible for a large memory grab)? It might actually help to see that big chunk of code :) Commented Feb 9, 2012 at 15:25
  • 128MB is allocated, Error: "<b>Fatal error</b>: Allowed memory size of 134217728 bytes exhausted (tried to allocate 5426168 bytes) in ..." It does in fact die at the same line every time, which I'm happy to admit is code related...I just don't know what else to do free up memory. This isn't something where I can say, oh we'll just increase the memory size and call it a day...because the size of the data being retrieved could be massive for some people. Commented Feb 9, 2012 at 17:38

1 Answer 1

1

This may be related to gc_collect_cycles. In some php versions and in some loop conditions this function will fail to collect the cycle: For instance, see: https://bugs.php.net/bug.php?id=53803 and https://bugs.php.net/bug.php?id=53071

If you can, try updating PHP to 5.3.9 and see if it works. If not, try dropping the collection and relying exclusively on unset() (keeping in mind that unsetting global variable only destroys it within the function) and you'll need to unset it from the $GLOBALS array if you want to destroy it everywhere.

If you're still stuck, try tracking your memory carefully as it moves through the loop by using memory_get_usage() with a note after anything that should require or release memory. Something like:

try{
    $result = $client->GetListingPhotosWithFullPath(array('mlsID'=>$bid->MlsID, 'mlsNumber'=>$bid->MlsNumber));
    echo "Got Photos with Full Path on line ".__LINE__." - Now using ".memory_get_usage()."<br />\n";
    $listing->photos = $result->GetListingPhotosWithFullPathResult->Photo;
    echo "Setting  $listing->photos on ".__LINE__." - Now using ".memory_get_usage()."<br />\n";
    (isset($opt->comet)) ? $msg("Photos received.") : '';

    //Memory Cleanup
    unset( $result );
    echo "\$result unseet on ".__LINE__." - Now using ".memory_get_usage()."<br />\n";
}

This should let you see where your cycle is building up memory.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I'm still stuck, but I think this is another question. The memory problem comes in with building the array out that I eventually write to a file. It just can't hold the array in memory apparently long enough for me to get the whole thing and write it to a file. :(
Can you try building out the file bit by bit rather than holding the whole thing in an array: file_put_contents ( $filename , $data, FILE_APPEND); and then unsetting that piece of data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.