致命错误:内存不足,但我确实有足够的内存(PHP) [英] Fatal error: Out of memory, but I do have plenty of memory (PHP)

查看:318
本文介绍了致命错误:内存不足,但我确实有足够的内存(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的问题越来越长,我决定重新编写整个问题,以使其变得越来越好.

Since my question is getting longer and longer, I decide to re-write the whole question to make it better and shorter.

我在具有8GB内存的专用服务器上运行我的网站.我完全意识到我需要提高php.ini设置的内存限制.我已将其从128M设置为256M,然后设置为-1.问题仍然是持久性.

I run my website on dedicated server with 8GB memory. I am fully aware that I need to raise the memory limit on php.ini setting. I have set it from 128M to 256M and to -1. Still the problem is persistence.

致命错误:内存不足(已分配786432)(尝试分配24576) 字节)在第81行的D:\ www \ football \ views \ main.php中

Fatal error: Out of memory (allocated 786432) (tried to allocate 24576 bytes) in D:\www\football\views\main.php on line 81

内存不足是没有道理的,因为它说只分配了786432字节,还需要24576字节.

The out of memory does not make sense because it said only 786432 bytes is allocated and it needed 24576 bytes more.

786432字节仅768 KB,并且很小.

786432 bytes is only 768 kilobytes and is fairly small.

  • 该错误发生在非常随机的行上.它并不总是在第81行出现错误.
  • 在高峰时间,Apache仅占用约500mb的内存.我还有6GB可用空间.
  • 没有无限循环.
  • 该脚本占用1,042,424字节.从echo memory_get_peak_usage();
  • 获取此号码
  • MySQL的结果集很小(最多12行,纯文本,无blob数据)
  • (重要)如果我每两天重启一次Apache,该错误就消失了.通常会在Apache运行超过2天时发生.
  • 我已经包含了该脚本的概要分析,您可以在此处获得它.
  • 此专用服务器仅用于运行一个网站.该网站是一个高流量的网站,平均每分钟有1,000位访问者.在高峰时间,将有1,700至2,000名访客同时访问.
  • The error occurs on a very random line. It does not always error on line number 81.
  • At peak time, Apache only takes around 500mb of memory. I still have 6GB to spare.
  • There is no infinite loop.
  • The script takes 1,042,424 bytes. Getting this number from echo memory_get_peak_usage();
  • The resultset from MySQL is small (at most 12 of rows, purely text, no blob data)
  • (Important) If I restart Apache once every two days, the error is gone. It usually happens when Apache is running more than 2 days.
  • I have included the profiling the script and you can get it here.
  • This dedicated server is purely used to run only one website. This website is a high traffic website with average of 1,000 visitors every minute. At peak time, there will be 1,700 to 2,000 visitors accessing at the same time.

操作系统:Windows 2008 R2 64位
CPU:Intel Core i5-4核
内存:8 GB
Apache 2.2
PHP 5.3.1
存储:2 x 1 TB硬盘驱动器
带宽:每月10 TB

OS: Windows 2008 R2 64-Bit
CPU: Intel Core i5 - 4 cores
RAM: 8 GB
Apache 2.2
PHP 5.3.1
Storage: 2 x 1 TB hard drives
Bandwidth: 10 TB per month

解决方案

我终于调好并解决了这个问题,我想在这里分享我为改善此问题所做的工作:

Solution

I have finally tuned up and fixed the problem and I would like to share it here what I have done to improve:

  1. favicon.ico丢失了,这弄乱了我的路由引擎.尽管我的路由引擎很小,但是通过包含favicon.ico,它可以通过不运行我的路由引擎来减少内存使用.我网站的大部分内容都有它,而我忘了在新部分中输入它.
  2. 限制MaxRequestPerChild有帮助.在我的其他专用服务器中,我的MaxRequestPerChild受限制.对于此服务器,我将其设置为0.我一直认为每个脚本都是隔离的.可以说我的脚本是否需要800kb才能运行.完成后,Apache或PHP应该释放800kb内存.看来这种方式行不通.受限制的MaxRequestPerChild确实可以通过在受限制的MaxRequestPerChild并且旧进程快要死之后创建新进程来帮助防止内存泄漏.这是我的新设置.

  1. favicon.ico was missing which mess up with my route engine. Although my route engine is very small, but by including favicon.ico, it helps reduce memory usage by not running my route engine. Most of part of my website has it and I forgot to put it for this new section.
  2. Limit MaxRequestPerChild helps. In my other dedicated server, I have my MaxRequestPerChild limited. For this server, I set it to 0. I always thought that each script is isolated. Lets say if my script takes 800kb to run. Upon its completion, Apache or PHP should free 800kb memory. It seem like it doesn't work this way. Limited MaxRequestPerChild does help to prevent memory leak by creating new process after limited MaxRequestPerChild and the old process is dying. This is my new setting.

ThreadsPerChild      1500
MaxRequestsPerChild  10000 

  • ob_flush();确实会减少更多的内存.它并没有多大帮助,但是优化的每一点都会有所帮助.

  • ob_flush(); does reduce slightly more memory. It does not help much but every bit of optimization helps.

    推荐答案

    在尝试使用交换时,我遇到了服务器死亡的同类问题.这是因为 mod_php永远不会释放内存.因此,Apache进程会不断增长,要么达到apache或PHP的内存限制,要么(如果没有限制)使服务器崩溃.

    I ran accross the same kind of problem with the server dying when trying to use the swap. This is because mod_php does not free memory ever. So Apache processes keep growing either reaching apache or PHP's memory limit or, if there's no limit, crashing the server.

    重新启动apache使其可以生成新的苗条进程,但是随着它们随着时间的推移运行PHP脚本,它们会不断增长,直到出现问题为止.

    Restarting apache makes it to spawn new fresh slim processes but as they run PHP scripts over time, they grow until problems arise.

    解决方案是在提供一定数量的查询后使Apache杀死进程,这样它将创建新的查询(有

    The solution is to make apache to kill processes after a certain number of queries served so it will create new ones ( There are some questions related to that) reducing the MaxRequestsPerChild configuration option to, let's say 100 (Defaults to 1000).

    当然,这可能会降低服务器性能,因为它需要资源来杀死和产生新进程,但至少可以使站点保持正常运行.您可能会想增加正在运行的进程数以保持较高的性能,请确保PHP(或Apache)内存限制x最大进程数不会超过服务器的物理内存.

    Of course this may reduce server performances as it takes ressources to kill and spawn new processes but at least it keeps the site working. You might be tempted to raise the number of running processes to keep performances high, be sure PHP (or apache) memory limit x max number of processes do not get over your server's physical ram.

    这是我的经验,希望能对您有所帮助.

    Here's my experience, hope it helps.

    这篇关于致命错误:内存不足,但我确实有足够的内存(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆