尝试创建大数组时 PHP 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2313893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with PHP when trying to create big array
提问by spacemonkey
Here is my code, which creates 2d array filled with zeros, array dimensions are (795,6942):
这是我的代码,它创建了一个用零填充的二维数组,数组维度是 (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?
任何想法如何解决这个问题?
回答by Pascal MARTIN
As a quick calculation, you are trying to create an array that contains :
作为快速计算,您正在尝试创建一个包含以下内容的数组:
795*6942 = 5,518,890
integers.
整数。
If we consider that one integer is stored on 4 bytes (i.e. 32 bits ; using PHP, it not be less), it means :
如果我们认为一个整数存储在 4 个字节上(即 32 位;使用 PHP,它不会更少),这意味着:
5518890*4 = 22,075,560
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
但不幸的是,事情并没有那么容易:-(
我想这与 PHP 使用内部数据结构存储数据有关,该结构比普通的 32 位整数复杂得多
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 :
现在,为了好奇,让我们修改您的函数,使其输出在每个外for循环结束时使用了多少内存:
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;
}
With this, I'm getting this kind of output (PHP 5.3.2-dev on a 64bits system ; memory_limitis set to 128MB-- which is already a lot !):
有了这个,我得到了这种输出(64 位系统上的 PHP 5.3.2-dev ;memory_limit设置为128MB- 这已经很多了!):
int 1631968
int 2641888
int 3651808
...
...
int 132924168
int 133934088
Fatal error: Allowed memory size of 134217728 bytes exhausted
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.
这意味着外循环的每次迭代都for需要大约 1.5 MB 的内存——而在脚本耗尽内存之前,我只能进行 131 次迭代;而不是你想要的 765。
Considering you set your memory_limitto 128M, you'd have to set it to something really much higher -- like
考虑到您设置memory_limit为128M,您必须将其设置为更高的值 - 就像
128*(765/131) = 747 MB
Well, even with
好吧,即使有
ini_set('memory_limit', '750M');
it's still not enough... with 800MB, it seems enough ;-)
这还不够……有了800MB,似乎就够了;-)
But I would definitly not recommend setting memory_limitto such a high value !
但我绝对不建议设置memory_limit这么高的值!
(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)
(如果您有 2GB 的 RAM,您的服务器将无法处理超过 2 个并发用户 ^^ ;; 老实说,如果我的计算机有 2GB 的 RAM,我实际上不会对此进行测试)
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)
(顺便说一句:也许“重新思考你的设计”意味着使用另一种语言 PHP:PHP 在开发网站方面很棒,但并不适合所有类型的问题)
回答by Kyle
The default PHP array implementation is very memory-intensive. If you are just storing integers (and lots of them), you probably want to look at SplFixedArray. It uses a regular contiguous memory block to store the data, as opposed to the traditional hash structure.
默认的 PHP 数组实现非常占用内存。如果您只是存储整数(以及很多整数),您可能想查看SplFixedArray。它使用常规的连续内存块来存储数据,而不是传统的哈希结构。
回答by CJD
You should try increasing the amount of memory available to PHP:
您应该尝试增加 PHP 可用的内存量:
ini_set('memory_limit', '32M');
in your PHP file.
在您的 PHP 文件中。

