2

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.

11
  • Can you elaborate a little further with more concrete examples between hosting environments? For instance, the same code with the same image on ABC hosting reports X memory, while on DEF it reports Y memory. This might help. Also, besides inconsistency, is there an actual problem? For instance, are you blowing a memory limit that you expect to be enforced? Commented Jan 9 at 3:39
  • Updated code for clarity. Commented Jan 9 at 6:10
  • I check if I can add emalloc, whatever that is... Commented Jan 9 at 6:32
  • @shingo but this comment is just about system memory usage and seems to be irrelevant to this question? Commented Jan 9 at 7:03
  • 1
    IIRC, PHP-FPM does have some notion of longer-running child processes, also Apache does something to avoid slow forking. It's not that simple. It's surely not something you should completely ignore. Commented Jan 9 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.