FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 02-08-2011, 11:27 AM
Jonas Mixter
 
Default 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') ."
";
echo "PHP memory usage before imagecreatetruecolor(): ";
echo memory_get_peak_usage() ." bytes
";
echo "Process memory usage before imagecreatetruecolor(): ";
echo process_memory_get_usage() ." MB
";

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

";
$foo = imagecreatetruecolor(10000,10000);

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


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4D5136C9.3000608@jamtport.se">http://lists.debian.org/4D5136C9.3000608@jamtport.se
 
Old 02-08-2011, 01:37 PM
Stan Hoeppner
 
Default PHP and memory_limit

Jonas Mixter put forth on 2/8/2011 6:27 AM:

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

Looks like you have a problem with OSX as well. Your test failed at allocating
40 KB when 128 MB was available. So, it's not failing at all on Debian, and
it's failing incorrectly on OSX.

I'd say you should hit a PHP mailing list or IRC channel. They'll likely know
this stuff better than anyone here.

BTW, if you're going to be processing images, there's not way around consuming
memory, thus you'll just have many fail if you set the memory limit low. That
doesn't get any work done.

--
Stan


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4D51551F.7010402@hardwarefreak.com">http://lists.debian.org/4D51551F.7010402@hardwarefreak.com
 
Old 02-08-2011, 03:04 PM
Jonas Mixter
 
Default PHP and memory_limit

Hi Stan!
Thank you for your answer.

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

Looks like you have a problem with OSX as well. Your test failed at allocating
40 KB when 128 MB was available. So, it's not failing at all on Debian, and
it's failing incorrectly on OSX.
PHP allocates memory in chunks until memory_limit has been exceeded. At
the time the script stopped executing, it was trying to allocate another
40 kB and probably used about 127,96 MB already.

I'd say you should hit a PHP mailing list or IRC channel. They'll likely know
this stuff better than anyone here.
This seems to be related to Debian since this works as expected in
Ubuntu (don't know the version) with PHP 5.2.14 and on another unknown
linux-box with PHP 5.3.5. I haven't been able to recreate the problem on
a non-Debian box.

BTW, if you're going to be processing images, there's not way around consuming
memory, thus you'll just have many fail if you set the memory limit low. That
doesn't get any work done.
The problem is that the processing doesn't fail. ;-) The thing is that
the code isn't mine, but the server is and I'd like to make sure that
the server cannot be abused like this.


Could you recreate the problem Stan? Or does the PHP code fail with an
error if you run it on lenny or squeeze?


Regards, Jonas Mixter


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Archive: 4D516974.6050607@jamtport.se">http://lists.debian.org/4D516974.6050607@jamtport.se
 
Old 02-09-2011, 05:23 AM
Stan Hoeppner
 
Default PHP and memory_limit

Jonas Mixter put forth on 2/8/2011 9:56 AM:

>> I'd say you should hit a PHP mailing list or IRC channel. They'll likely know
>> this stuff better than anyone here.

> This seems to be related to Debian since this works as expected in Ubuntu (don't
> know the version) with PHP 5.2.14 and on another unknown linux-box with PHP
> 5.3.5. I haven't been able to recreate the problem on a non-Debian box.

Nevertheless, that is where you should go for help. Do you think such folks run
php on every platform except Debian? On the XFS list we field and respond to
questions from OPs running every Linux distro, and turn none away. Granted XFS
is kernel level software, but it is part of every Linux system. So is php.

Again, the places I mentioned are your best bet for assistance. Never be shy
about this.

--
Stan


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4D5232C4.1040909@hardwarefreak.com">http://lists.debian.org/4D5232C4.1040909@hardwarefreak.com
 
Old 02-09-2011, 06:15 AM
"Boyd Stephen Smith Jr."
 
Default PHP and memory_limit

In <4D5136C9.3000608@jamtport.se>, Jonas Mixter wrote:
>--- 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

I'd be interested to see what memory_get_peak_usage() returns after
imagecreatetruecolor() is called on OSX. I'm thinking perhaps that value is
over 128M on OSX and it is that tracked memory that is being limited by PHP.

As far as the discrepancy between memory_get_peak_usage() and
process_memory_get_usage(), that seems to occur on both OSes, so I'd ask in a
PHP-specific forum.
--
Boyd Stephen Smith Jr. ,= ,-_-. =.
bss@iguanasuicide.net ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-'
http://iguanasuicide.net/ \_/
 

Thread Tools




All times are GMT. The time now is 06:09 PM.

VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org