PHP:内存不足异常,但我已经更改了memory_limit

PHP:内存不足异常,但我已经更改了memory_limit

问题描述:

I was getting this error:

PHP Fatal error: Allowed memory size of X bytes exhausted

So, after some research I changed my php.ini file:

memory_limit: 2048M

The previous error message stoped but I get this error instead:

Fatal error: Out of memory (allocated 1746927616) (tried to allocate 268435456 bytes) in [path]

So I've got 2048M as memory limit and in my error the php file allocated 1666M and tried to allocate 256M which give 1922M. This value is smaller than my memory limit so why I am getting this error? (I've checked the php info in my IIS site and the memory limit is the one I mention before).

Thanks :)

EDIT:

Right before the line in which this error ocorred I did a

echo memory_get_usage();

And I got:

1675848

The code where's the error is this:

for ($i = $startKey; $i <= $endKey; $i++) {
        array_push($paginas, $files[$i]);
}

我收到此错误: p>

  PHP致命错误:  X字节的允许内存大小耗尽
  code>  pre> 
 
 

因此,经过一些研究后我改变了我的php.ini文件: p>

  memory_limit:2048M 
  code>  pre> 
 
 

上一条错误消息已停止但我收到此错误: p>

 致命错误: 内存不足(分配1746927616)(试图分配268435456字节)[path] 
  code>  pre> 
 
 

所以我有2048M的内存限制,在我的错误中php 文件分配1666M并试图分配256M,给出1922M。 这个值小于我的内存限制,为什么我收到此错误? (我已经检查了我的IIS站点中的php信息,内存限制是我之前提到过的。) p>

谢谢:) p>

编辑 : p>

在发生此错误的行之前我做了 p>

  echo memory_get_usage(); 
  code>  
 
 

我得到了: p>

  1675848 
  code>  pre> 
 
 

代码在哪里 错误是这样的: p>

  for($ i = $ startKey; $ i&lt; = $ endKey; $ i ++){
 array_push($ paginas,$ files [$ i]  ]); 
} 
  code>  pre> 
  div>

Well thank you for your tips. It's actually a code-design problem. This isn't my code (I've started working in a already existing project) so it's been a little harder do debug and to decide if I look for some configuration error or assume the code is bad. When I first searched the error I found so many people with the same problem that I stupidly rulled out bad code (and the error was hapening in a part that deals with files).

So, in my case and as suggested here, I start looking my code and used the

echo memory_get_usage();

To see if all the memory was really been allocated in that line of code and find out that was true. So then I checked the loop variables and sometimes the first one wasn't initalized corretly and the consequence was a no-ending loop.

Thank you all and sorry for the trouble :)

When you allocate memory, you are actually requesting a contiguous chunk. So, what probably is happening is that have the total memory required, but it's split in two or more chunks.