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>");
?>
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With