PHP 致命错误:允许的内存大小已耗尽 - Drupal 7

问题描述 投票:0回答:1

我检查了 php-fpm 日志并收到以下消息:

PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 28672 bytes) in /home/....com/public_html/.../includes/cache.inc on line 361
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 262144 bytes) in /home/....com/public_html/.../includes/database/database.inc on line 731
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 16384 bytes) in /home/....com/public_html/.../includes/cache.inc on line 449
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 16384 bytes) in /home/....com/public_html/.../includes/cache.inc on line 449
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 16384 bytes) in /home/....com/public_html/.../includes/cache.inc on line 449
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 20480 bytes) in /home/....com/public_html/.../includes/cache.inc on line 449
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 16384 bytes) in /home/....com/public_html/.../includes/cache.inc on line 449
 PHP Fatal error:  Allowed memory size of 4294967296 bytes exhausted (tried to allocate 32768 bytes) in /home/....com/public_html/.../includes/database/database.inc on line 2284
# function contains line 361
 function getMultiple(&$cids) {
    try {
      // Garbage collection necessary when enforcing a minimum cache lifetime.
      $this->garbageCollection($this->bin);

      // When serving cached pages, the overhead of using db_select() was found
      // to add around 30% overhead to the request. Since $this->bin is a
      // variable, this means the call to db_query() here uses a concatenated
      // string. This is highly discouraged under any other circumstances, and
      // is used here only due to the performance overhead we would incur
      // otherwise. When serving an uncached page, the overhead of using
      // db_select() is a much smaller proportion of the request.
      $result = db_query('SELECT cid, data, created, expire, serialized FROM {' . db_escape_table($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids));
      $cache = array();
// #361     
 foreach ($result as $item) { 
        $item = $this->prepareItem($item);
        if ($item) {
          $cache[$item->cid] = $item;
        }
      }
      $cids = array_diff($cids, array_keys($cache));
      return $cache;
    }
    catch (Exception $e) {
      // If the database is never going to be available, cache requests should
      // return FALSE in order to allow exception handling to occur.
      return array();
    }
  }
#function contains line 449
protected function prepareItem($cache) {
    global $user;

    if (!isset($cache->data)) {
      return FALSE;
    }
    // If the cached data is temporary and subject to a per-user minimum
    // lifetime, compare the cache entry timestamp with the user session
    // cache_expiration timestamp. If the cache entry is too old, ignore it.
    if ($cache->expire != CACHE_PERMANENT && variable_get('cache_lifetime', 0) && isset($_SESSION['cache_expiration'][$this->bin]) && $_SESSION['cache_expiration'][$this->bin] > $cache->created) {
      // Ignore cache data that is too old and thus not valid for this user.
      return FALSE;
    }

    // If the data is permanent or not subject to a minimum cache lifetime,
    // unserialize and return the cached data.
//#449
    if ($cache->serialized) {
      $cache->data = unserialize($cache->data);
    }

    return $cache;
  }

我当前的php.ini文件如下:

PHP version:    7.4.33
PHP memory_limit:   4G
PHP post_max_size:  16M
PHP upload_max_filesize:    4M
PHP max_input_vars: 1000
PHP max_execution_time: 60
And:
OS: Almalinux 9,
webserver: Apache
Drupal version: V 7.101

我尝试将 php.ini 中的 memory_limit 参数调整为 7GB 并收到以下错误消息

PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 32768 bytes) in /home/.....com/public_html/..../includes/cache.inc on line 361
PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 20480 bytes) in /home/.....com/public_html/..../includes/cache.inc on line 449
PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 20480 bytes) in /home/.....com/public_html/..../includes/cache.inc on line 449
PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 20480 bytes) in /home/.....com/public_html/..../includes/cache.inc on line 449
PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 20480 bytes) in /home/.....com/public_html/..../includes/cache.inc on line 449
PHP Fatal error:  Allowed memory size of 7516192768 bytes exhausted (tried to allocate 262144 bytes) in /home/.....com/public_html/..../includes/database/database.inc on line 731

我不明白错误来自哪里,请帮助我。我尝试通过设置 memory_limit = -1 来更改它,但我的服务器在 24 小时后崩溃了

drupal-7
1个回答
0
投票

我同意其他海报的观点。 问题源于此查询产生的任何内容:

$result = db_query('SELECT cid, data, Created, Expire, Serialized FROM {' . db_escape_table($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $ cids));

您需要找到一种方法来分析该查询,因为它显然会生成一个结果集,该结果集的大小相对于您的个人查询限制 (16Mb) 和应用程序总内存占用 (4Gb) 来说几乎是无限的。 如果您在数据字段中存储媒体(即 4K 影片剪辑),您可以轻松超出这些内存限制。

我的建议是打开 MySQL 并开始探索。

如果你不是后端类型的人,看一下被选中的列:

选择 cid, # 通常是主键(小) data, # 通常是记录负载(大) 创建,#通常是一个时间戳(小) expire, # 通常是一个时间戳(小) Serialized # 通常是序列化数据(BIG)

您只有两个嫌疑人需要检查...都与数据有关:

所以,对我来说,您的记录有效负载可能很大,因为有 50/50 的机会您没有存储指向数据的指针,而是存储数据本身。

© www.soinside.com 2019 - 2024. All rights reserved.