dq23171 2017-04-13 19:22
浏览 68
已采纳

PHP内存限制

I'm trying to test memory allocation in a php script. I have the limit set to 128M in php.ini. In the script, ini_get('memory_limit') reports 128M as the limit. The problem is, when I start allocating space I can't go beyond 23M. I can restrict memory allocation to any level below 23M but can't allocate more. Just looking for clues as to where this limit is being imposed.

Here's my test script...

<?php

echo "System imposed memory_limit is set to: " . ini_get('memory_limit') . "<br>
";
for ($i=1; $i<1000; $i++) {
  $a = loadmem($i);
  echo "You have allocated ". $i . "M (". memory_get_usage() . ") memory in this php script" . "<br />
";
  unset($a);
}

function loadmem($howmuchmeg) {
  $a = str_repeat("0", $howmuchmeg * 1024 * 1024); // alocating 10 chars times million chars
  return $a;
}

?>

Here's the output using curl...

System imposed memory_limit is set to: 128M<br>
You have allocated 1M (1403632) memory in this php script<br />
You have allocated 2M (2452232) memory in this php script<br />
You have allocated 3M (3500808) memory in this php script<br />
You have allocated 4M (4549384) memory in this php script<br />
You have allocated 5M (5597960) memory in this php script<br />
You have allocated 6M (6646536) memory in this php script<br />
You have allocated 7M (7695112) memory in this php script<br />
You have allocated 8M (8743688) memory in this php script<br />
You have allocated 9M (9792264) memory in this php script<br />
You have allocated 10M (10840848) memory in this php script<br />
You have allocated 11M (11889424) memory in this php script<br />
You have allocated 12M (12938000) memory in this php script<br />
You have allocated 13M (13986576) memory in this php script<br />
You have allocated 14M (15035152) memory in this php script<br />
You have allocated 15M (16083728) memory in this php script<br />
You have allocated 16M (17132304) memory in this php script<br />
You have allocated 17M (18180880) memory in this php script<br />
You have allocated 18M (19229456) memory in this php script<br />
You have allocated 19M (20278032) memory in this php script<br />
You have allocated 20M (21326608) memory in this php script<br />
You have allocated 21M (22375184) memory in this php script<br />
You have allocated 22M (23423760) memory in this php script<br />
You have allocated 23M (24472336) memory in this php script<br />

Here are the relevant error_log entries...

mmap() failed: [12] Cannot allocate memory
PHP Fatal error:  Out of memory (allocated 2097152) (tried to allocate 25165856 bytes) 

Using php 7.0.x and apache2.2

  • 写回答

2条回答 默认 最新

  • doulin2025 2017-04-13 19:29
    关注

    The error indicates that you don't actually have that much memory available. If you had simply exceeded the limit, you'd get a slightly different message:

    PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried...
    

    Your issue could be a result of actual memory available or a shell-imposed ulimit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢