试图创造大阵时,使用PHP的问题 [英] Problems with PHP when trying to create big array
问题描述
下面是我的code,这将创建用零填充二维数组,数组尺寸为(795,6942):
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);
这是我收到的错误:
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 :
795*6942 = 5,518,890
整数。
如果我们考虑到一个整数存储在4个字节的 的,它的意思是:
If we consider that one integer is stored on 4 bytes (i.e. 32 bits ; using PHP, it not be less), it means :
5518890*4 = 22,075,560
字节。
OK,快速计算......结果是的应该是确定的。
OK, quick calculation... result is "it should be OK".
但事情并非那么容易,不幸:-(
结果的我想这是与该数据是由PHP使用内部数据结构,它比整数一个普通的32位复杂得多存储的事实的
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
结果
现在,仅仅是好奇,让我们来修改你的函数,所以它输出了多少内存在外的每一个为
-loop年底时:
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 :
function zeros($rowCount, $colCount){
$matrix = array();
for ($rowIndx=0; $rowIndx<$rowCount; $rowIndx++){
$matrix[] = array();
for($colIndx=0; $colIndx<$colCount; $colIndx++){
$matrix[$rowIndx][$colIndx]=0;
}
var_dump(memory_get_usage());
}
return $matrix;
}
有了这个,我得到这种输出的(PHP中的64位系统上5.3.2-dev的; memory_limit的
设置为 128MB
- 这已经很多了)的:
With this, I'm getting this kind of output (PHP 5.3.2-dev on a 64bits system ; memory_limit
is set to 128MB
-- which is already a lot !) :
int 1631968
int 2641888
int 3651808
...
...
int 132924168
int 133934088
Fatal error: Allowed memory size of 134217728 bytes exhausted
这意味着外部的每个迭代为
-loop需要像1.5 MB的内存 - 我只得到131迭代脚本内存用完之前;而不是像765你想要的。
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.
考虑到你设置你的 memory_limit的
到 128M
,你必须将其设置为一些真正高得多 - - 像
Considering you set your memory_limit
to 128M
, you'd have to set it to something really much higher -- like
128*(765/131) = 747 MB
好吧,即使有
ini_set('memory_limit', '750M');
这还不够...与 800MB
,似乎不够; - )
it's still not enough... with 800MB
, it seems enough ;-)
结果
但我definitly不建议设置 memory_limit的
来这么高的价值!
的(如果你有2GB的RAM,您的服务器将无法处理超过2个并发用户^^ ;;我不会实际测试这一点,如果我的电脑有2GB的RAM,说实话) 的
结果
我在这里看到的唯一的解决办法是让你重新考虑你的设计:必须有别的东西,你可以做得比使用code的这一部分: - )
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:也许重新思考你的设计意味着使用另一种语言PHP:PHP是伟大的,当谈到深化发展网站,但并不适合于每一种问题)的
这篇关于试图创造大阵时,使用PHP的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!