PHP running out of memory in all my scripts
EDIT:
My php.ini has 256MB memory set:
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 250 ; Maximum execution time of each script, in seconds
max_input_time = 120 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 256MB ; Maximum amount of memory a script may consume (256MB)
So I had a certain PHP script which was not very well written and when I executed it the PHP ran out of memory and my PC froze. Before running the script I have increased the m开发者_运维问答emory limit in php.ini. I have changed it back to the default value after.
Now the problem is it seems to have done something to my PHP installation. Every PHP script I execute now is telling me it has not enough memmory. The scripts that have worked before without an issue.
It seems like the one bad script I mentioned earlier is still running in the background somehow.
I have restarted PHP, Apache, I have restarted my PC and even went to sleep for 8 hours. The next thing in the morning I find out all PHP scripts are still running out of memory. What the hell?
I am getting errors like this everywhere now (with the file in the error changing of course) - with every single even the simplest PHP script:
Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 6144 bytes) in D:\data\o\WebLib\src\Db\Db.php on line 241
Fatal error (shutdown): Allowed memory size of 262144 bytes exhausted (tried to allocate 6144 bytes) in D:\data\o\WebLib\src\Db\Db.php on line 241
Ok here is the script (I have commented out the bad parts):
<?php
error_reporting(E_ALL);
define('BASE_PATH', dirname(__FILE__));
require_once(BASE_PATH.'/../WebLib/config/paths.php');
require_once(PATH_TO_LIB3D_SRC.'/PHPExcel/Classes/PHPExcel.php');
require_once(PATH_TO_LIB3D_SRC.'/PHPExcel/Classes/PHPExcel/Reader/IReadFilter.php');
///** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
//class chunkReadFilter implements PHPExcel_Reader_IReadFilter {
// private $_startRow = 0;
// private $_endRow = 0;
// /** Set the list of rows that we want to read */
// public function setRows($startRow, $chunkSize)
// {
// $this->_startRow = $startRow;
// $this->_endRow = $startRow + $chunkSize;
// }
// public function readCell($column, $row, $worksheetName = '')
// {
// // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
// if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) {
// return true;
// }
// return false;
// }
//}
//
//function ReadXlsxTableIntoArray($theFilePath)
//{
// $arrayData =
// $arrayOriginalColumnNames =
// $arrayColumnNames = array();
//
// $inputFileType = 'Excel2007';
// /** Create a new Reader of the type defined in $inputFileType **/
// $objReader = PHPExcel_IOFactory::createReader($inputFileType);
// /** Define how many rows we want to read for each "chunk" **/
// $chunkSize = 10;
// /** Create a new Instance of our Read Filter **/
// $chunkFilter = new chunkReadFilter();
// /** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
// $objReader->setReadFilter($chunkFilter);
// $objReader->setReadDataOnly(true);
// /** Loop to read our worksheet in "chunk size" blocks **/
// /** $startRow is set to 2 initially because we always read the headings in row #1 **/
// for ($startRow = 1; $startRow <= 65536; $startRow += $chunkSize) {
// /** Tell the Read Filter, the limits on which rows we want to read this iteration **/
// $chunkFilter->setRows($startRow,$chunkSize);
// /** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
// $objPHPExcel = $objReader->load($theFilePath);
// // Do some processing here
//
// $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
// foreach($rowIterator as $row){
//
// $cellIterator = $row->getCellIterator();
// //$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
// if(1 == $row->getRowIndex ()) {
// foreach ($cellIterator as $cell) {
// $value = $cell->getCalculatedValue();
// $arrayOriginalColumnNames[] = $value;
// // let's remove the diacritique
// $value = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $value);
// // and white spaces
// $valueExploded = explode(' ', $value);
// $value = '';
// // capitalize the first letter of each word
// foreach ($valueExploded as $word) {
// $value .= ucfirst($word);
// }
// $arrayColumnNames[] = $value;
// }
// continue;
// } else {
// $rowIndex = $row->getRowIndex();
// reset($arrayColumnNames);
// foreach ($cellIterator as $cell) {
// $arrayData[$rowIndex][current($arrayColumnNames)] = $cell->getCalculatedValue();
// next($arrayColumnNames);
// }
// }
//
// unset($cellIterator);
// }
//
// unset($rowIterator);
// }
//
// // Free up some of the memory
// $objPHPExcel->disconnectWorksheets();
// unset($objPHPExcel);
//
// return array($arrayOriginalColumnNames, $arrayColumnNames, $arrayData);
//}
//
//if (isset($_POST['uploadFile'])) {
// //list($tableOriginalColumnNames, $tableColumnNames, $tableData) = ReadXlsxTableIntoArray($_FILES['uploadedFile']['tmp_name']);
// //CreateXMLSchema($tableOriginalColumnNames, 'schema.xml');
// //echo GetReplaceDatabaseTableSQL('posta_prehlad_hp', $tableColumnNames, $tableData);
//}
Change the php.ini :
memory_limit = 256M
Note that you're not supposed to use MB or KB, but M or K
PHP expects the unit Megabyte to be denoted by the single letter M. You've specified 256MB. Notice the extra B.
Since PHP doesn't understand the unit MB, it falls back to the lowest known "named" unit: kilobyte (K).
Simply remove the extra B from your setting and it should properly read the value as 256 megabyte (256M)
Please see the following FAQ entry on data size units:
PHP: Using PHP - Manual
You have a memory_limit of 256K
. Thats much to less in nearly all cases. The default value is 16M
(since 5.2).
Are you sure you set your memory size back correctly? The error shows that your max memory is 262144 bytes
, and that is a quarter of an MB. That's really low!
As reaction to your php settings: shouldn't that syntax be
memory_limit = 256M
I don't know if it accepts both M and MB, but it might not?
Hey Richard. The changes couldn't have been executed since PHP clearly states that you only have 256K
set as limit. Look through the php.ini and all the other places. It could be located in a .htaccess file on the vhost/host.
Have you restarted Apache after you edited php.ini and increased the memory_limit = 256MB
?
精彩评论