4

I need to update a value in an array inside a loop. Normally this works fine, but something about this loop is causing the memory to max out.

while ($watering_window['total_run_time'] > $water_window_total)
{
    foreach ($master_schedule as &$_ms)
    {
      $current_soak_time = $_ms['between_starts'] - $_ms['total_run_time'];
      if ($current_soak_time > 0)
      {
          $new_soak_time = ceil($current_soak_time * 0.9);
          $_ms['between_starts'] = $_ms['total_run_time'] + $new_soak_time;
          $watering_window['total_run_time'] -= $current_soak_time - $new_soak_time;
      }
    }
}

Something about writing to $_ms['between_starts'] (an existing key) is causing the memory problem. If I change it to write a NEW key (like $_ms['between_starts_new'], no memory problem. I also tried writing a constant value to it (instead of something mathy) and it still timed out.

11
  • doesn't look like the braces for the if ($current_soak_time > 0) are closed Commented Apr 22, 2016 at 16:21
  • Thank you, jcorry. That was a transcription error. In my actual code there's a conditional that shows the user a message if the current soak time is not > 0. Commented Apr 22, 2016 at 16:23
  • 1
    Is it an infinite loop? Does $watering_window['total_run_time'] > $water_window_total ever evaluate to false? Using a debugger makes troubleshooting stuff like this so much easier... Commented Apr 22, 2016 at 16:27
  • 1
    Have you tried removing the reference to your &$_ms value? It could be what's causing $_ms['between_starts'] to balloon in size (continuous increment), and in turn, cause $current_soak_time to also balloon, thus not allowing $watering_window['total_run_time'] > $water_window_total to ever return false. Or do you require $_ms to be referenced for other reasons? Commented Apr 22, 2016 at 16:44
  • 1
    $watering_window['total_run_time'] is supposed to be getting smaller, but it can't because $_ms['between_starts'] keeps getting bigger and bigger. And in turn, because of that, as does $current_soak_time. That's how I see it, anyway. You'll have to rewrite your logic and determine whether the reference is actually necessary in this case, because it's directly affecting the outcome of your loop(s). Commented Apr 22, 2016 at 17:01

2 Answers 2

3

You'll need to increase the memory_limit. Add the following at the top of your php script:

<?php
ini_set('memory_limit','256M');
...

Adjust the value according to your needs.

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

3 Comments

My memory limit is currently set to 134,217,728 bytes. There must be a way to solve the problem without changing the memory limit.
Yes, you'll probably need to change your code but I don't have the slightest idea of what you're trying to do...
Yeah, there's a ridiculous amount of domain knowledge in this code. I hoped that whatever I'm doing wrong would be obvious enough that understanding what the code does wouldn't matter... Essentially I'm iteratively reducing an amount of time until the conditions of the while loop are met. Each time I reduce it, I need to update the array in case that's the last time it gets updated. Maybe there's a better way to do this last part?
2

Try unsetting the key that is causing the problem before you set it.

foreach ($master_schedule as &$_ms)
{
  $current_soak_time = $_ms['between_starts'] - $_ms['total_run_time'];
  if ($current_soak_time > 0)
  {
      $new_soak_time = ceil($current_soak_time * 0.9);
      unset($master_schedule['between_starts']);
      $_ms['between_starts'] = $_ms['total_run_time'] + $new_soak_time;
      $watering_window['total_run_time'] -= $current_soak_time - $new_soak_time;
  }

}

5 Comments

Great suggestion. It doesn't seem to have resolved the memory issue though. I tried it with your latest edit, too.
@JessycaFrederick, have you tried dumping the value of $_ms while it's looping to see if it's growing? Have you also tried doing it without using a reference?
I don't think I have tried either method because I'm not sure what you mean. (I'm not an experienced programmer.)
I've tried unsetting the value on its own (as you've shown) without the subsequent write statement and it creates an infinite loop.
Oh, ok. So instead of using something like echo do var_dump($_ms) before the error occurs in the code. It should print the contents of the array so you can see what is happening during the execution of the code. If you don't know what I'm talking about with the reference, don't worry about it for now. Just find out if the $_ms array is growing, or if any other arrays are growing. Because that is what is most likely causing the issue.

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.