Here is my code, which creates 2d array filled with zeros, array dimensions are (795,6942):
function zeros($rowCount, $colCount){
$matrix = array();
for ($rowIndx=0; $rowIndx<$rowCount; $rowIndx++){
$matrix[] = array();
for($colIndx=0; $colIndx<$colCount; $colIndx++){
$matrix[$rowIndx][$colIndx]=0;
}
}
return $matrix;
}
$matrix = zeros(795,6942);
And here is the error that I receive:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes)
Any ideas how to solve this?
As a quick calculation, you are trying to create an array that contains :
integers.
If we consider that one integer is stored on 4 bytes (i.e. 32 bits ; using PHP, it not be less), it means :
bytes.
OK, quick calculation… result is “it should be OK“.
But things are not that easy, unfortunatly 🙁
I suppose it’s related to the fact that data is stored by PHP using an internal data-structure that’s much more complicated than a plain 32 bits integer
Now, just to be curious, let’s modify your function so it outputs how much memory is used at the end of each one of the outer
for-loop :With this, I’m getting this kind of output (PHP 5.3.2-dev on a 64bits system ;
memory_limitis set to128MB— which is already a lot !) :Which means each iteration of the outer
for-loop requires something like 1.5 MB of memory — and I only get to 131 iterations before the script runs out of memory ; and not 765 like you wanted.Considering you set your
memory_limitto128M, you’d have to set it to something really much higher — likeWell, even with
it’s still not enough… with
800MB, it seems enough 😉But I would definitly not recommend setting
memory_limitto such a high value !(If you have 2GB of RAM, your server will not be able to handle more than 2 concurrent users ^^ ;; I wouldn’t actually test this if my computer had 2GB of RAM, to be honest)
The only solution I see here is for you to re-think your design : there has to be something else you can do than use this portion of code 🙂
(BTW : maybe “re-think your design” means using another language PHP : PHP is great when it comes to developping web-sites, but is not suited to every kind of problem)