PHP Fatal error: Allowed memory size of exhausted

Created on 10 July 2024, about 2 months ago
Updated 13 July 2024, about 2 months ago

Problem/Motivation

PHP Fatal error: Allowed memory size of 4294967296 bytes exhausted

I checked the php-fpm log and received the following message:

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

My current php.ini file is as follows:

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

I tried adjusting the memory_limit parameter in php.ini to 7GB and received the following error message

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

Please help me!!

🐛 Bug report
Status

Postponed: needs info

Version

7.0 ⚰️

Component
Cache 

Last updated 1 day ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Postponed: needs info about 2 months ago
  • 🇺🇸United States cilefen

    Welcome to the Drupal Community. What you see there isn't normal. When PHP runs out of memory it can be difficult to interpret the error line because that is just the point at which PHP completely ran out of memory. The cause is sometimes elsewhere.

    But reports like this require steps to reproduce. Because this bug report is missing those I am postponing the issue on more information.

  • I also don't understand what's going on. After I updated from version 7.94 to version 7.101, the php-fpm log reported the same. At first I thought it was because the memory_limit parameter was not enough, so I changed it from 1024MB -> 3GB but that message still PHP Fatal error appeared, and I continued to change from 3GB->4GB but the message still appeared in the log.

  • 🇺🇸United States cilefen

    Probably no amount of allocated memory will be enough as there is likely a code issue here. But you have not added steps to reproduce to this issue. For example, we don't even know on which page, or even on which kind of page, your error occurs. We don't know which modules are installed, and so on.

  • Because I don't know which module is causing the error, I'm temporarily fixing it in the following way
    in includes/cache.inc

    function getMultiple(&$cids) {
        try {
            // Garbage collection necessary when enforcing a minimum cache lifetime.
            $this->garbageCollection($this->bin);
    
            $cache = array();
            $batchSize = 1000; // Adjust the size of each batch
    
            foreach (array_chunk($cids, $batchSize) as $chunk) {
                $result = db_query('SELECT cid, data, created, expire, serialized FROM {' . db_escape_table($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $chunk));
    
                foreach ($result as $item) {
                    $item = $this->prepareItem($item);
                    if ($item) {
                        $cache[$item->cid] = $item;
                    }
                }
            }
    
            // Update the $cids array to only include those that were not found.
            $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();
        }
    }
    /////
    protected function prepareItem($cache) {
        global $user;
    
        if (!isset($cache->data)) {
            return FALSE;
        }
    
        if ($cache->expire != CACHE_PERMANENT && variable_get('cache_lifetime', 0) && isset($_SESSION['cache_expiration'][$this->bin]) && $_SESSION['cache_expiration'][$this->bin] > $cache->created) {
            return FALSE;
        }
    
        if ($cache->serialized) {
            $memory_limit = ini_get('memory_limit');
            $memory_usage = memory_get_usage();
    
            // Chuyển đổi giá trị $memory_limit sang byte
            $memory_limit_bytes = $this->convertToBytes($memory_limit);
    
            if ($memory_usage + strlen($cache->data) > $memory_limit_bytes * 0.9) { // Limit 90% memory
                return FALSE;
            }
            $cache->data = unserialize($cache->data);
        }
    
        return $cache;
    }
    
    private function convertToBytes($value) {
        $unit = strtolower(substr($value, -1));
        $bytes = (int) $value;
    
        switch($unit) {
            case 'g':
                $bytes *= 1024 * 1024 * 1024;
                break;
            case 'm':
                $bytes *= 1024 * 1024;
                break;
            case 'k':
                $bytes *= 1024;
                break;
        }
    
        return $bytes;
    }
    
    
Production build 0.71.5 2024