|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-08-21 01:28 UTC] myteamz at rogers dot com
Description:
------------
I am creating a script to produce a PNG image of a chart showing common ancestors of several individuals, using the gd functionality supplied with php. After processing the input, including many calls to MySQL (version 5.0.2) all running on Apache 2.2 on Windows XP SP2, I am trying to allocate the image resource. This works for very small trees, but fails on all calls with large trees.
The PHP error message is:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 13770 bytes) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\GenChart\drawgraph.php on line 116
The offending code is:
$im = imagecreate ($xsize, $ysize);
Reproduce code:
---------------
Lots of memory allocation/freeing is occurring in the script prior to this call. I have included several debug lines prior to the offending call, as below:
$memMax = ini_get("memory_limit");
$memUsed = memory_get_usage(true);
$xMax = round(30000000-intval($memUsed/$ysize),-3);
$xsize = min($xMax,max($xsize,4000));
dumpData("have used $memUsed of $memMax memory");
dumpData("trying to create image of size ($xsize,$ysize)");
$im = imagecreate ($xsize, $ysize);
The output, on crash, is
have used 6815744 of 32M memory
trying to create image of size (13770,990)
Expected result:
----------------
I have changed the script to produce standard HTML output for debugging purposes, which allows me to get this info. When running as intended, with a small tree/image, the script should and does run successfully, creating an image, and returning it as a .PNG image. Running the same debug code for a small image (with no crash), the output is:
have used 6553600 of 32M memory
trying to create image of size (4000,690)
Actual result:
--------------
When running in debug mode for a large tree/image, the debug output is:
have used 6815744 of 32M memory
trying to create image of size (13770,990)
When running as intended, the script fails and no image is returned to the browser.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 07:00:01 2025 UTC |
On looking further, both imagecreate andimagecreatetruecolor were working, and use different amounts of memory. However, the amounts are different than 4 bytes in both cases. For imagecreate, 2 bytes (+marginal overhead) are allocated. For imagecreatetruecolor, 5 bytes (+about .15 per pixel) are allocated. These were calculated using the following script; <?php for ($i=0;$i<2;$i++) { println("iteration = $i"); $mUsed = memory_get_usage(true); $mPeak = memory_get_peak_usage(true); println(" $mUsed, $mPeak"); $xSize=8192; $ySize=1024; if ($i==0) $im = imagecreate($xSize,$ySize); else $im = imagecreatetruecolor($xSize,$ySize); $mUsed2 = memory_get_usage(true); $mPeak2 = memory_get_peak_usage(true); println(" $mUsed2, $mPeak2"); $iUsed = $mUsed2-$mUsed; $perPixel = $iUsed/($xSize*$ySize); println(" incremental memory - $iUsed ($perPixel per pixel)"); imagedestroy($im); $mUsed3 = memory_get_usage(true); $mPeak3 = memory_get_peak_usage(true); println(" $mUsed3, $mPeak3"); println; } function println($text) {print("$text\n<BR>");} ?> From this, it appears there is 1 or 4 bytes for color, plus 1 byte for something else. This should be in the docs. Consider the item closed.