[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

PHP and memory_limit



Hi list!
I've got a problem with PHP and memory_limit. It seems like the the memory usage in the following example isn't limited at all, even though memory_limit is set. I've tried this on both lenny and squeeze with the standard php.ini. Libgd (gd.so) is included as an extension and also from the standard Debian package.

When running the following example, it seems like PHP doesn't know how much memory is used by the PHP process. In php.ini, memory_limit is set to 32 MB, PHP thinks it uses ~57 kB of memory. That's all good, but the process uses more than 330 MB of memory when imagecreatetruecolor() is finished! Please see the output below.

I've also tested the same script on OS X (version 10.6.6, PHP 5.3.3) and the execution is aborted when the memory usage is running off. Just as I would anticipate. I'm not seeking advice on OS X... I'm just providing the output as comparison.

Of course, this isn't any problem if the PHP process uses only 300 MB of memory. But this will also work with larger images and memory usage of several GB. I've managed to take down a server completely with just this single line of PHP code.

Could this be because libgd is loaded as an extension? Is this behaviour then by design? Is there anyone else on the list that could reproduce the same result? I've tested on several Debian boxes at the office, and they all happily let's me use way more memory that allowed.

Best regards, Jonas Mixter


--- Example output from Debian machines ---
# php ./mem_test
PHP memory limit before imagecreatetruecolor(): 32M
PHP memory usage before imagecreatetruecolor(): 58280 bytes
Process memory usage before imagecreatetruecolor(): 6 MB

Creating image... Will allocate ~300 MB of memory

PHP memory limit after imagecreatetruecolor(): 32M
PHP memory usage after imagecreatetruecolor(): 67064 bytes
Process memory usage after imagecreatetruecolor(): 338 MB


--- Example output from OS X ---
$ php ./mem_test
PHP memory limit before imagecreatetruecolor(): 128M
PHP memory usage before imagecreatetruecolor(): 644976 bytes
Process memory usage before imagecreatetruecolor(): 8 MB

Creating image... Will allocate ~300 MB of memory

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40000 bytes) in /Users/jonas/mem_test on line 20


--- The PHP-file mem_test.php ---
<?
function process_memory_get_usage()
{
        $pid = getmypid();
        exec("ps -o rss -p $pid", $output);
        return (int) ($output[1] / 1024);
}

echo "PHP memory limit before imagecreatetruecolor(): ";
echo ini_get('memory_limit') ."\n";
echo "PHP memory usage before imagecreatetruecolor(): ";
echo memory_get_peak_usage() ." bytes\n";
echo "Process memory usage before imagecreatetruecolor(): ";
echo process_memory_get_usage() ." MB\n";

echo "\nCreating image... Will allocate ~300 MB of memory\n\n";
$foo = imagecreatetruecolor(10000,10000);

echo "PHP memory limit after imagecreatetruecolor(): ";
echo ini_get('memory_limit') ."\n";
echo "PHP memory usage after imagecreatetruecolor(): ";
echo memory_get_peak_usage() ." bytes\n";
echo "Process memory usage after imagecreatetruecolor(): ";
echo process_memory_get_usage() ." MB\n";
?>


Reply to: