admin管理员组

文章数量:1125954

The problem here by PHP code:

Problem server(nginx/1.26.2 with php-fpm8.2, 8.2.26 (cli), Zend Engine v4.2.26):

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$file="/var/www/html/test2.jpg";
$src_img=imagecreatefromjpeg($file); // completely ignores this commands memory usage of 300M+

print 'Memory usage: '. round(memory_get_peak_usage(true)/(1024*1024)) . 'M<br />'; //returns 2M

(this reports 2M)

Working server(apache, PHP 8.3.14 php83-cgi,Zend Engine v4.3.14):

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
$file="/var/www/html/test2.jpg";
$src_img=imagecreatefromjpeg($file); // works correctly 300M+

print 'Memory usage: '. round(memory_get_peak_usage(true)/(1024*1024)) . 'M<br />'; //returns 300M+

(this reports 300M+)

Text:

I have PHP php-fpm8.2 with nginx working (Debian 12, VPS). But on this VPS the memory_get_peak_usage does not count the imagecreatefromjpeg(); memory usage, that is about 300M+, and so php.ini memory_limit is also ignored of 10M (because php reports only 2M), if out of memory(because real usage is 300M+) PHP service is terminated (if available memory is 300 or less system will terminate php service).

This script works on some other shared Apache server and displays the memory usage correctly about 300M, but on this VPS it shows only 2M. Linux top command also shows about 300M memory usage.

Why the memory_get_peak_usage behaves wrongly on the VPS.

If anyone has any experience please share. Thank you!

*I upgraded my "Problem server" to 8.3.15 with default php.ini, still ignoring GD memory.

*To reproduce the error: Install fresh Debian 12 (VPS provided that for me SMP PREEMPT_DYNAMIC Debian 6.1.119-1, 6.1.0-28-amd64, x86_64). I installed default Debian packages nginx and PHP(not Apache version), install php-gd, run the code test.php, or just use command line(tested on 2 different VPS Debian 12 servers):

sudo php /var/www/html/test.php

reports Memory usage: 2M

(You can use normal size image on your test, or use my image, GD library module uses a lot of memory)

*This is a huge problem, if someone would upload small but huge resolution image to this server that takes 3+gigs of server memory, the PHP would let it, and the website/app will crash with the PHP service, out of memory.

*setting memory limits in php.ini or www.conf are ignored, since PHP-FPM does not know how much memory the script uses(since it uses a PHP modul).

*Only workaround: Use getimagesize(), and check that the resolution is not too high, before using the GD image library.

本文标签: fpmPHP memorygetpeakusage ignores memory usageStack Overflow