php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41245 Ability to set handler for "memory limit exceeded"
Submitted: 2007-04-30 19:33 UTC Modified: 2014-12-30 21:10 UTC
Votes:12
Avg. Score:4.0 ± 1.4
Reproduced:7 of 8 (87.5%)
Same Version:3 (42.9%)
Same OS:6 (85.7%)
From: bens at effortlessis dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 7.0 OS: Any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bens at effortlessis dot com
New email:
PHP Version: OS:

 

 [2007-04-30 19:33 UTC] bens at effortlessis dot com
Description:
------------
When Memory Limit is exceeded, PHP simply terminates, which shows as a "blank white screen". 

It would be very helpful if a callback function or simply an optional header redirect could be emitted when memory limit is exceeded. It could be as simple as an option in php.ini which would work like the "ErrorDocument" apache directive, so that an attractive, "What you asked for used up too much memory, so sorry!" page could be displayed rather than just an uninformative and unfriendly blank screen.

This idea could perhaps be expanded to a default "PHP died" handler, so that a friendly "something went wrong" page could be displayed. 

Reproduce code:
---------------
$somevar=''; 
for ($i=0; $i<2000000000000; $i++) 
 $somevar.="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 

Expected result:
----------------
Program output: 
Location: /value/set/in/php.ini/or/apache/directive/helpful.html

Logfile output:
[client 63.195.17.22] PHP Fatal error:  Allowed memory size of 100663296 bytes exhausted (tried to allocate 11870154 bytes) in /path/to/php/file.php on line 216, referer: http://mysite.com/path/to/php/file.php?PHPSESSID=2a39d521e3660f0a4fc2132dc34e1e68

Actual result:
--------------
Program output: 

Logfile output:
[client 63.195.17.22] PHP Fatal error:  Allowed memory size of 100663296 bytes exhausted (tried to allocate 11870154 bytes) in /path/to/php/file.php on line 216, referer: http://mysite.com/path/to/php/file.php?PHPSESSID=2a39d521e3660f0a4fc2132dc34e1e68

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-04 19:58 UTC] slusarz at curecanti dot org
This would be tremendously useful, especially when the cause of the memory overrun is not the PHP code but rather due to the data input to the script.

For example: In Horde/IMP, we need to parse MIME e-mail messages.  These e-mail messages contain data that potentially could be unfiltered.  A particular mail message script MUST parse the entire data input in order to show anything (you cannot truncate MIME data, or else the resulting MIME message is invalid).  Even using things like temporary data streams, there is still a chance that the memory limit can be exceeded.

It would be tremendously useful to be able to define a memory limit exceeded handler for any given script that could catch the case where we can not parse the message due to a memory limit caused by the message data itself.  This handler could allow the script to do something like automatically redirect the browser to a different page and display an error message.
 [2012-09-04 20:18 UTC] rasmus@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2012-09-04 20:18 UTC] rasmus@php.net
That is rather tricky because such a callback could continue consuming memory. We 
would have to introduce the concept of soft and hard limits or something along 
those lines for this to be realistic.
 [2012-09-04 20:28 UTC] rasmus@php.net
Oh, and why wouldn't you just use a shutdown function for this? If you do all 
your processing before sending any output and then check for an out of memory in 
the shutdown function you can easily do this redirect in this case.
 [2014-12-30 21:10 UTC] tyrael@php.net
-PHP Version: 4.4.6 +PHP Version: 7.0
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 13 04:01:26 2024 UTC