6

I am trying to create a 2D array in PHP with a size of 2000x2000 (4 million entries). It seems that I run out of memory here, but the manner in which the error is appearing is confusing me.

When I define the array and fill it initially using the array_fill command, and initialize each position in the array (matrix) with 0 there is no problem.

However if I try iterating over the array and fill each position with 0, it runs out of memory.

I would assume that once I run array_fill it allocates the memory at that point, and it should not run out of memory in the loop.

Of course, this is just a simplified version of the code. In my actual application I will be using the X & Y coordinates to lookup value from another table, process it, and then store it in my matrix. These will be floating point values.

Can somebody help through some light on this please? Is there some other way I should be doing this?

Thank you!

<?php

// Set error reporting.
error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

// Define Matrix dimensions.
define("MATRIX_WIDTH", 2000+1);
define("MATRIX_HEIGHT", 2000+1);


// Setup array for matrix and initialize it.
$matrix = array_fill(0,MATRIX_HEIGHT,array_fill(0,MATRIX_WIDTH,0));

// Populate each matrix point with calculated value.
for($y_cood=0;$y_cood<MATRIX_HEIGHT;$y_cood++) {

    // Debugging statement to see where the script stops running.
    if( ($y_cood % 100) == 0 )  {print("Y=$y_cood<br>"); flush();}

    for($x_cood=0;$x_cood<MATRIX_WIDTH;$x_cood++) {
        $fill_value = 0;
        $matrix[$y_cood][$x_cood]=$fill_value;
    }
}

print("Matrix width: ".count($matrix)."<br>");
print("Matrix height: ".count($matrix[0])."<br>");

?>
2
  • Try to increase PHP memory size memory_limit = 128M ; Maximum amount of memory a script may consume (128MB). I personally do not advise to believe the results which caused by unstable script behavior. Commented Nov 18, 2014 at 7:52
  • Thanks. Did that eventually. Commented Nov 20, 2014 at 6:48

1 Answer 1

4

I would assume that once I run array_fill it allocates the memory at that point, and it should not run out of memory in the loop.

Yes ...and no. Allocating memory and executing the program code are two different shoes (usually).
The memory allocated to a program/process is usually divided in two - heap and stack. When you "allocate memory" (in the meaning you used in your question), this occurs in the heap. When you execute program code, the stack is also used. Both are not completely separated, since you may push and/or pop references (pointers to the heap) on and/or from the stack.

The thing is that the heap and the stack share part of the memory (allocated to that process) and usually the one grows (is being filled) from higher addresses to the low ones and the other - from low addresses to the higher one, and so you have a "floating" border between both. As soon as both parts reach that "border" you're "out of memory".
enter image description here

So, in your case, when you create and fill your array(matrix) you've used memory for 2001 x 2001 integers. If an integer requires 32 bits or 4 Bytes, then there are 2001 x 2001 x 4 Bytes = 4004001 x 4 Bytes = 16016004 Bytes ~ 16 MB.
When executing the code, the stack's being filled with the (local) variables - loop condition variable, loop counter and all the other variables.
You should also not forget that the PHP (library) code should also be loaded in the memory, so depending on the value you have set as memory_limit in your configuration, you may quickly run out of memory.

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

Comments

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.