php-src icon indicating copy to clipboard operation
php-src copied to clipboard

"Out of memory" fatal error when apache memory size is big

Open jwwq opened this issue 4 years ago • 1 comments

Description

when Apache process has big size (memory was allocated earlier), a fresh new script can't allocate relatively small chunk of memory and gives "Fatal error: Out of memory".

This bug description is based on earlier bug #81585 "cached_chunks are not counted to real_size on shutdown" (https://bugs.php.net/bug.php?id=81585). That bug was fixed (thanks [email protected]), but when I checked the test script with "memory limit = -1", it fails. Looks like there is another problem.

The script to reproduce the problem is the same as in #81585 (https://pastebin.com/qh8hneTg), but you should set "memory_limit" to "-1" in line 39. It allocates memory for many small strings, and when memory really ends it stops with "Fatal error: Out of memory". That's ok. Run it again, and Apache process grows bigger. That's ok too, afaik PHP doesn't compulsively free all memory when script finishes, to reuse it next time faster, so Apache process stays big even there are no scripts running. But if you run that script 6-7 times and Apache is already 1.6G, the script will try to alloc 200M in one chunk at the start (all memory is really free at the moment, no active scripts), and you'll get "Out of memory" right away and that's not ok. See output:

apache size: 1602 mb, memory usage/real usage: 0/1584 mb (0 items in array, 0 mb stored)
apache overloaded (1602 mb), trying to alloc 200 mb

Fatal error: Out of memory (allocated 1660944384) (tried to allocate 209715224 bytes) in bug_test.php on line 57

before crash:
apache size: 1602 mb, memory usage/real usage: 0/1584 mb (0 items in array, 0 mb stored)

(Please see #81585 bug description to get more information if needed)

I think it's a bug because it's a new run of script, no memory is occupied by previous runs or another scripts, and PHP definitely can allocate 200 mb on totally free memory.

(I don't know exactly how PHP allocation algorithm works, maybe PHP doesn't update some allocation info when previous script runs end with error. It's just a guess.)

(system: Windows, Apache 2.4 x32, php 8.1.4 x32)

PHP Version

8.1.4

Operating System

Windows

jwwq avatar Apr 09 '22 19:04 jwwq

This does not look like a bug. The problem is that the PHP memory statistics are per thread, while the memory allocation is constrained per process (especially on x86). So if you're running Apache mod_winnt with multiple worker threads (ThreadsPerChild etc.), this issue can occur. Unless you have a particular reason to run 32bit Apache, I suggest to use a 64bit version.

cmb69 avatar Apr 12 '22 13:04 cmb69