Avoid memory overflow in a PHP function

Asked

Viewed 608 times

0

Guys, I’m opening here and I have the following problem: I created a function with a hint that I found right here in stackoverflow.

function DozenGenerate($game,$dozen) {
    $this->game = $game;
    $this->dozen = $dozen;
    if ($game === 0) {
        return array(array());
    }
    if (count($dozen) === 0) {
        return array();
    }
    if (count($dozen)< $game){
    }
    ob_start();
    $dozen_receive = $dozen[0];
    $dozen_compare = array_slice($dozen,1,count($dozen)-1);
    $dozenI = $this->DozenGenerate($game-1,$dozen_compare);
    for ($i = 0; $i < count($dozenI);$i++){
        array_splice($dozenI[$i],0,0,$dozen_receive);
    }
    ob_get_contents();
    $dozenII = $this->DozenGenerate($game,$dozen_compare);
    ob_end_flush();
    return array_merge($dozenI,$dozenII);
    }

Then I create a range and use the function:

$range = range(1,24);
$numbers = DozenGenerate(15,$range);
print_r($numbers);

When it generates very large unique combinations, it returns this error that would be natural:

Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 25165832 bytes) in C:\xampp\htdocs\netboloes\class\folk.php on line 44

I wanted to know how to do the memory emptying so that the function continues running, without returning this message and yes the array? I hope you can help me.

    1. You can change the amount of memory in php.ini 2. You can declare at the beginning of the ini_set('memory_limit','16M'); code this would be a paliactive output... or ini_set('memory_limit','-1'); if you did not want a memory limit.
  • Vlw Fernando by the tip. But in practice this would not be possible with the code running on a server, as it would not have access to php.ini directives...

  • Hello, you need this array for some future data processing or just to print on the screen as it is in the sample code?

  • In this case the array will be printed on the screen and will then be available for application of a series of filters

  • Do you have access to the database, so you can save this data from the array? Because if it were just for screen printing, I would have a very simple solution, but having to manipulate data, it is necessary bd

  • David is my question. This function can generate literally millions of indexes within an array. After generating all unique combinations, filters apply that will significantly reduce these indexes to then be saved by the user in the BD

  • Got it. Can the filtering process be done during the generation? Thus decreasing the array?

  • Unfortunately I did not see this possibility. This pq would generate an even greater delay in the return of the function. That’s why I opted for the filters application after the array generation.

  • Your recursion will always tend to consume all available memory. You’d better try some technique dynamic programming to reduce both the execution time (which I imagine to be enormous) and memory consumption.

  • Could this problem be solved? I have the same problem when consuming Rbbitmq messages. There comes a time that gives this same error

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.