PHP - Memory Allocation Error
Hello everyone,
I made this website using CakePHP, and it was working great, but then suddenly it started giving me this error Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 38 bytes) in /cake/libs/model/datasources/dbo/dbo_mysql.php on line 766 Here is the code that contains line number 766 Code: [Select] /** * Fetches the next row from the current result set * * @return unknown */ function fetchResult() { if ($row = mysql_fetch_row($this->results)) { $resultRow = array(); $i = 0; foreach ($row as $index => $field) { list($table, $column) = $this->map[$index]; $resultRow[$table][$column] = $row[$index]; $i++; } return $resultRow; } else { return false; } } /** Similar TutorialsHey, was just writing rough code for a news feed script, and this error came up: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693565 bytes) in /Applications/XAMPP/xamppfiles/htdocs/cameo/newsfeed.php on line 17 here's the code <?php require_once("vars.php"); //write the query to get the news feed info from the database $query = "SELECT * FROM news_feed"; $result = mysqli_query($dbc, $query); //create beginning tags for the news data table $output = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\" name=\"news_data\">"; //display the news feed data $row = mysqli_fetch_array($result); while($row){ $output .= "<tr>" ."<td><h3>".$row['post_title']."</h3>" ."<br />Posted by: ".$row['posted_by']." on ".$row['date']."</td>" ."</tr>" ."<tr>" ."<td>".$row['post_body']."</td>" ."</tr>"; } $output .= "</table>"; echo $output; ?> and heres my vars file if needed <? //common session variables $username = $_SESSION['username']; $is_admin = $_SESSION['is_admin']; //database connection information variables $server = "localhost"; $user = "root"; $pass = ""; $db = "cameo"; //database function variables $dbc = mysqli_connect($server, $user, $pass, $db); //http header and script return variables $index = "/cameo/index.php"; $self = $_SERVER['PHP_SELF']; ?> Hi, I'm getting following error on my website Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36000000 bytes) in /var/www/vhosts/celebratingart.com/1DEV_CELEBRATINGART/judges/phpimageeditor/classes/phpimageeditor.php on line 652 I tried to increase memory limit to 256M but invain. Please Help I'm having real difficulty with something that should simply work... I've got a script that reads files within a specific folder defined elsewhere. Then it returns the filenames (image files) to an array. Relatively simple, you'd think... Here's the process loop : Code: [Select] // Read directory for images @$dh = opendir($img_dir); while (false !== (@$filename = readdir($dh))) { if (($filename == '.') || ($filename == '..') || ($filename == 'Thumbs.db')) { } else { $files[] = $filename; } } There is a maximum of 4 images in each folder. This process will run once per page load. I have checked my php settings: memory_limit 256M post_max_size 64M upload_max_filesize 62M (just for good measure). IMHO 256m works for another site I developed which processes up to three images using imagemagick, and I've tested this with a 4mb png file too.... Yet I get this error : Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 35 bytes) in F:\html\bishopstrings\components\com_content\views\article\tmpl\default.php on line 43 This is telling me it's trying to allocate 35bytes when there is 268435456 bytes available... Yet this fails - any help Hi I have an upload script which uploads an image then resizes The error Code: [Select] Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 7776 bytes) in Dir/UploadScript.php on line 234 The code function $strNewFileName = date("YmdHis").rand(0,5000).".".$this->strFileExt; $this->strNewFileName = $strNewFileName; $strDir = $_SERVER['DOCUMENT_ROOT'].$this->strSaveDir.$strNewFileName; $strTNDir = $_SERVER['DOCUMENT_ROOT'].$this->strSaveDir."tn_".$strNewFileName; move_uploaded_file($this->arrUploadFile['tmp_name'], $strDir); $image_p = imagecreatetruecolor($this->intNewWidth, $this->intNewHeight); switch($this->strFileExt){ case "jpg": $image = imagecreatefromjpeg($strDir); break; case "gif": $image = imagecreatefromgif($strDir); break; case "png": $image = imagecreatefrompng($strDir); break; } imagecopyresampled($image_p, $image, 0, 0, 0, 0, $this->intNewWidth, $this->intNewHeight, $this->intCurWidth, $this->intCurHeight); $blnSuccessUpload = false; switch($this->strFileExt){ case "jpg": if(imagejpeg($image_p, $strTNDir)){ $blnSuccessUpload = true; } break; case "gif": if(imagegif($image_p, $strTNDir)){ $blnSuccessUpload = true; } break; case "png": if(imagepng($image_p, $strTNDir)){ $blnSuccessUpload = true; } } imagedestroy($image_p); return $blnSuccessUpload; the Line causing issues (234) is Code: [Select] $image = imagecreatefromjpeg($strDir); Now that image is created but the TN is not //Edit Just a note. I have ini_set('memory_limit', 16MB); if you notice the allocated memory is less than the memory allowed. Hello, I have no experience when dealing with large files so I am not sure what to do about this. I have attempted to read several large files using file_get_contents ; the task is to clean and munge them using preg_replace(). My code runs fine on small files ; however, the large files (40 MB) trigger an Memory exhausted error: Code: [Select] PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41390283 bytes) I was thinking of using fread() instead but I am not sure that'll work either. Is there a workaround for this problem? Thanks for your input. Al. Hi again, I did a simple request and i get right response from curl_exec($ch); but when i call the static method str_get_html my result is always the same. QuoteFatal error: Allowed memory size of 536870912 bytes exhausted I tried increase the memory memory_limit=2048M but the result is the same. Proof of memory limit: Memory Limit I also tried ->clear(); unset(); but doesn't work. I'm using the following library: kub-at/php-simple-html-dom-parser My code: $url = "https://www.php.net"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, []); $this->callback = HtmlDomParser::str_get_html(curl_exec($ch)); .... Any tips? I can use regular expression, but will be my last choice. Thank you! Rage. The whole error message reads: Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 16 bytes) Dunno bout you but that doesn't make sense to me! Anywho I am constantly getting this error when any PHP variables are stored or anything. I'm guessing thats the case coz its saying the error is on line 6 which is: $card = rand(1,52); I tried a search on google but just got confused, is there a simple explanation why this is happening? thanks alot guys I'm getting a fatal memory error and have been unable to figure out what is causing it. To make it worse, the allowed memory size does not appear to be related in any way to the memory_limit value. Here's the information: Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 128 bytes) in template.php on line 164 Excerpt from template.php (line 164 is where $max is assigned): ... else if(isDateType($type) || isTimestampType($type) || isDatetimeType($type)) { $min = htmlentities($countpost['constraints'][$tablename][$fieldname]['min'], ENT_QUOTES); $max = htmlentities($countpost['constraints'][$tablename][$fieldname]['max'], ENT_QUOTES); echo "\t\tFrom: <input name=\"constraints[$tablename][$fieldname][min]\"" ." type=text value=\"$min\" onclickXSSCleaned=\"toggleCalendar(this)\" readonly>\n"; echo "\t\tTo: <input name=\"constraints[$tablename][$fieldname][max]\"" ." type=text value=\"$max\" onclickXSSCleaned=\"toggleCalendar(this)\" readonly>\n"; } ... These two lines were included at the top of the script and printed "Base Memory Usage: 473172 Memory Limit: -1": echo "Base Memory Usage: ". memory_get_usage() ."\n"; echo "Memory Limit: ". ini_get('memory_limit') ."\n"; I also get the same error in a slightly different location: Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 1 bytes) in template.php on line 153 Excerpt from template.php (line 153 is the simple variable assignment of an empty string): ... if(!isset($countpost['constraints'][$tablename][$fieldname]['value'])) $countpost['constraints'][$tablename][$fieldname]['value'] = ""; ... If I refresh the page repeatedly, the error will predictably occur at the same place. Once I change any code (even slightly), the location of the error will change to a new location, sometimes into an included file. The size of "tried to allocate X bytes" also changes. The only constant in this whole mess is the allowed memory size (which is ALWAYS 262144, no matter what the actual memory limit is). I had previously set memory_limit to -1 in the php.ini file, which seems to have no effect on the error. I am not using a .htaccess file. Changing the memory_limit doesn't change the "allowed memory size" whether it's set to 16M, 64M or whatever. I've restarted apache after each change in php.ini. I have memory_get_usage in the code several times to watch the memory. There are small changes, and a change of about 300k when I first load another file. The max gets up to 1048576 bytes (memory_get_peak_usage). 1 Mb does not seem very large to me and is much smaller than the memory_limit. Also, today I wrote a short php script to just use up memory. This was just to test php and see if my installation is messed up. It worked just fine and I got up to 128Mb used without encountering the error above. So it's something in my code. This is using php 5.3.2, apache2 2.2.14, on Ubuntu 10.04. Any insights would be greatly appreciated. I have a wordpress site that has this error in my error logs and seems to be causing the site to load very slow: PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 64 bytes) in /home/name/public_html/wp-includes/meta.php on line 339 this is line 339: Code: [Select] $cache[$mpid][$mkey] = array(); a couple line above and below: Code: [Select] if ( !empty($meta_list) ) { foreach ( $meta_list as $metarow) { $mpid = intval($metarow[$column]); $mkey = $metarow['meta_key']; $mval = $metarow['meta_value']; // Force subkeys to be array type: if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) $cache[$mpid] = array(); if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) $cache[$mpid][$mkey] = array(); // Add a value to the current pid/key: $cache[$mpid][$mkey][] = $mval; } } Can some please help this is causing major issues. Hi. I tried to add some more if statements to check for validation and I'm now getting this error. The "on line ###" changes depending how many fields are FALSE. Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 35 bytes) in /home/content/f/u/s/fusionfashion/html/registration.php on line 133 I think it has to do with the php.ini file. So I added the memory_limit in the php.ini file. Code: [Select] register_globals = off allow_url_fopen = off expose_php = Off max_input_time = 60 variables_order = "EGPCS" extension_dir = ./ upload_tmp_dir = /tmp precision = 12 memory_limit = 128M post_max_size = 128M SMTP = relay-hosting.secureserver.net url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset=" [Zend] zend_extension=/usr/local/zo/ZendExtensionManager.so zend_extension=/usr/local/zo/4_3/ZendOptimizer.so But I'm still getting the error. How to I check my php version and this value within a browser? <?php phpinfo() ?> Found this. Its showing 64M in LOCAL and MASTER. But I just updated the local? Hi , As you may all have experienced this problem is there way we can non-healthy exit of script ..due to memory problem........??? I got to know..we avoid this......by properly managing our php memory by unset function. remove unusable variables and objects from memory...... Is there any way we can check each time inside the script -to check memory is available or sleep for some time and check again.......to proceed without exit........ Is there a way to catch this fatal error and take necessary steps to avoid exit........ Thanks, redfort :confused: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4864 bytes) in /home/a5418385/public_html/scripts/status.php on line 2 What does this mean? I have a question on when PHP will allocate space in the code. Does it happen up front, or as the code executes? // some stuff if (some_conditional) { // something happens here and returns or exits } // we never get here $foo = array(); exit(0); In this example, does $foo get created? I am in the midst of developing a web application and despite having a decent amount of experience in web development, I never really stopped and gave a ton of thought to memory when it comes to my php scripts. However, with this project I am using zend framework and it is a decent memory hog. So I have some questions... I am monitoring my memory use for a typical call to my site and it appears as though its somewhere around 20MB. This seems like a ton and I am actively looking for ways to decrease it. One thing I noticed is that when I call zend's date class for the first time, my memory usage shoots up 3MB. However it is pretty much untouched for other instances of this class. So if I do Code: [Select] echo(memory_get_usage()); echo('<br />'); $this->last_login=Zend_Date::now(); echo(memory_get_usage()); echo('<br />'); $this->last_active=Zend_Date::now(); echo(memory_get_usage()); echo('<br />'); The result is Quote 9136448 12812408 12814496 So the jump is with that first call. My question is, if two users are accessing the site at the same time, will they both experience that 3MB memory use? Or will the server have that class in memory so it only uses 3MB once? Basically, if a call to the site uses 20MB of memory, will it be 20MB * the number of users. Or will the server have a lot of the information loaded into memory to subsequent calls so multiple users would use up far less memory then the initial? I would assume the latter is the case based on the fact that reusing zend_date doesn't incur a 3MB hit every time. But without being admin of my server and hvaing tons of folks to test at once, its hard to tell. Just how big can i make this ? Im sure it depends on the server hardware and if thats the case then how does one fidn out how big I can make this. I want to max it out. -SB I have a 2-D array that holds a table of information that is stored in a $_SESSION array variable. If all the information in the table is correct, then it should be added to a database. If not correct, it will be sent to a page to be modified. There are a total of three pages: AddInformation.php ProcessInformation.php EditInformation.php Table array is created from a form on AddInformation.php. ProcessInformation.php checks for the validity of the information. And EditInformation.php allows you to update the errored information. A $_SESSION array that is Array[5][9] stops at ProcessInformation.php and doesn't bothering using the header('Location: EditInformation.php') that it is supposed to use. The only difference between the start memory usage and the end memory usage is of 5kb. As far as I know there is enough memory on the operating machine. Is there a way to fix this? Edit: I do not have direct access to the server or its terminal. Hey guys i have been making a website where friends can post on each others profiles like facebookish anyways im not sure if this is th ebest way to do it but i have this below its part of the code that pulls all the friends of the member from the database:: $query_friend = mysql_query("SELECT my_id,friend_id FROM site_friends WHERE my_id='$my_id'"); while($row = mysql_fetch_assoc($query_friend)){ $friend_id = $row['friend_id']; } then i pass it into a function blah blah not got that for yet anyway what happens if this person ends up having 100s of friends will this be harsh on the servers memory? or should i not worry to much ? hope someone can help thanks!! What happens if you do not unset an array before the script is done executing? I am running through thousands of CSV files, parsing data for hundreds of thousands of customers. It works fine for the first 5/6 hours then starts bogging down bad. I run about 5-10 CSVs per execution...I'm wondering if unsetting the arrays in the script would help this or not...I thought they would be unallocated after the script ends. Am I wrong? I have this method that puts files into a zip file from an array list, it looks like this: public function zip($files = array(), $maxMB = 2, $flag = ZIP_NEW){ $this->memory_size = $maxMB * 1024 * 1024; $zip_id = $this->zip_id; $this->zip[] = new ZipArchive(); $this->zip_id++; if($flag == ZIP_NEW){ $open = $this->zip[$zip_id]->open("php://temp/maxmemory:$this->memory_size", ZipArchive::CREATE); $files = (array)$files; if(!empty($files)){ foreach($files as $file){ if(is_file($file)){ $this->zip[$zip_id]->addFile($file); } } } $this->zip[$zip_id]->close(); } $this->function_name = __FUNCTION__; return $this; } I then download them like this: public function download($download_data, $download_name, $type = DOWNLOAD_FILE){ header("Pragma: public;"); header("Expires: 0;"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0;"); header("Content-Type: application/force-download;"); header("Content-Type: application/octet-stream;"); header("Content-Type: application/download;"); header("Content-Transfer-Encoding: binary;"); header("Content-Disposition: attachment; filename=$download_name;"); if($type == DOWNLOAD_FILE){ header("Content-Length: ".filesize($download_data).";"); if($download_data == PHP_MEMORY){ $handle = fopen(PHP_MEMORY."/maxmemory:$this->memory_size", 'r+'); echo stream_get_contents($handle); }else{ readfile($download_data); } }elseif($type == DOWNLOAD_STRING){ header("Content-Length: ".mb_strlen($download_data).";"); echo $download_data; } $this->function_name = __FUNCTION__; return $this; } When I run this code: $files = array("file1.php","file2.php"); $live->zip($files)->download(PHP_MEMORY, "download.zip"); It downloads the file, the only problem is, is that when I open the file I get an error saying the file can not be opened. I know when I actually save it to a file instead of in memory it works. So, I am not sure if it isn't working in the download portion or the create file portion, as I have never worked with php://temp before. Any thoughts on why this doesn't work when using php://temp? Hello Guys, I wanted to know what is memory leakage? Where we have to takecare of ? How to avoid this thing? |