Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:18:30+00:00 2026-05-31T11:18:30+00:00

I have a script that sometimes break because I get an error like: PHP

  • 0

I have a script that sometimes break because I get an error like:

PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) in ...

The memory limit is NOT too low and was put at 128M. (This should be more than enough.)

The problem is that as the script goes, it takes more and more memory. Basically, it’s a for loop. I’ve checked every variable but there is no place where the script fills ‘something’ (for example, an array) that would grow and explain why the script takes more memory.

I confirmed the memory problem using: memory_get_usage(). I’ve printed out the memory footprint after each iteration and the number is always higher.

Using Xdebug does show not any sign that can help me at this point.

Is there any way I could drilldown and know what is taking more memory everytime I loop ? What would you suggest to debug that situation ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:18:31+00:00Added an answer on May 31, 2026 at 11:18 am

    The short answer is: it’s not possible to drilldown.

    That said, the way I found what the problem was is that I splitted the code inside the loop in subfunctions. Then, I commented them all to only check what was the footprint of looping without doing anything. Then, I’ve uncommented function by function until I found the problematic one.

    Once I’ve got the problematic function, I did the same process again: comment everything in it then uncomment until I find the problematic piece of code.

    I finally found that I was calling a function that used create_function from PHP. Many people are ‘complaining’ about ‘memory leaks’ from this function. The problem is not a memory leak but instead the fact that if you call the create_function in a loop, it will really create as many functions as the number of times you loop. To avoid this, I’ve found this concept to avoid recreating the function thousands of times.

    <?php 
    global $my_func; 
    if (!isset($my_func)) { 
        $my_func = create_function($args, $code); 
    }
    
    $my_func(); 
    ?>
    

    Adapting the code to make sure the function is created only once solved the problem. The whole script now takes only 8MB of memory instead of breaking after busting over 128MB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one script that sometimes gives Max execution times reached error. This is
I have a PHP script that is running and I sometimes just want to
I have a Perl script, that's supposed to match this string: Sometimes, he says
I have a PHP script that is using get_dns_record to retrieve and display specific
I have a PHP script that runs a SELECT query then immediately deletes the
I have a script that runs every 15 minutes but sometimes if the box
I have a PHP script that moves files out of a specific folder on
I have a php-script that uploads files to an ftp server from a location
So I have an external script that will sometimes input something into a sql
I have a cron script that outputs errors using error_log() . Sometimes, however, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.