PHP - What Is Memory Leakage?
Hello Guys,
I wanted to know what is memory leakage? Where we have to takecare of ? How to avoid this thing? Similar TutorialsJust 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 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. I have osTicket installed on a Centos 5 Os with php5.3.3
I upgraded the osTicket from v1.9.1 to 1.9.4
I now get the following mail when I create a ticket by mailing in the support ticket
This is the mail system at host mail.mydomain.com.
I'm sorry to have to inform you that your message could not be delivered to one or more recipients. It's attached below.
For further assistance, please send mail to <postmaster>
If you do so, please include this problem report. You can delete your own text from the attached returned message.
The mail system
<support@mydomain.com>: Command died with status 255: "/usr/bin/php -q
/var/www/html/support/api/pipe.php". Command output: PHP Fatal error:
Allowed memory size of 268435456 bytes exhausted (tried to allocate 76
bytes) in /var/www/html/support/include/class.osticket.php on line 261 PHP
Warning: Unknown: Error occured while closing statement in Unknown on line
0
The osticket guys have pointed out to me that this is a php error, but I never made any changes to the php config
When I increase the memory in php.ini to 512Mb or even 1024Mb the error persists.
Anyone have any ideas of whether the memory is set/can be set somewhere else or overriding what is in php.ini ?
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!! 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? Hi: How do I set up an input textbox so that it remembers the text a user entered? Code: [Select] <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>" > <input name="myguess" type="text" value=""> <input type="hidden" name="allTheGuesses" value = ""> <input type="submit" value="GUESS"> </form> I can't seem to figure it out 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. 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 stepped into more than one project with a function similar to the following: Code: [Select] <? /** * xml2array() will convert the given XML text to an array in the XML structure. * Link: http://www.bin-co.com/php/scripts/xml2array/ * Arguments : $contents - The XML text * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. * Examples: $array = xml2array(file_get_contents('feed.xml')); * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); */ function xml2array($contents, $get_attributes=1, $priority = 'tag') { if(!$contents) return array(); if(!function_exists('xml_parser_create')) { //print "'xml_parser_create()' function not found!"; return array(); } //Get the XML parser of PHP - PHP must have this module for the parser to work $parser = xml_parser_create(''); xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, trim($contents), $xml_values); xml_parser_free($parser); if(!$xml_values) return;//Hmm... //Initializations $xml_array = array(); $parents = array(); $opened_tags = array(); $arr = array(); $current = &$xml_array; //Refference //Go through the tags. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array foreach($xml_values as $data) { unset($attributes,$value);//Remove existing values, or there will be trouble //This command will extract these variables into the foreach scope // tag(string), type(string), level(int), attributes(array). extract($data);//We could use the array by itself, but this cooler. $result = array(); $attributes_data = array(); if(isset($value)) { if($priority == 'tag') $result = $value; else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode } //Set the attributes too. if(isset($attributes) and $get_attributes) { foreach($attributes as $attr => $val) { if($priority == 'tag') $attributes_data[$attr] = $val; else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' } } //See tag status and do the needed. if($type == "open") {//The starting of the tag '<tag>' $parent[$level-1] = &$current; if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag $current[$tag] = $result; if($attributes_data) $current[$tag. '_attr'] = $attributes_data; $repeated_tag_index[$tag.'_'.$level] = 1; $current = &$current[$tag]; } else { //There was another element with the same tag name if(isset($current[$tag][0])) {//If there is a 0th element it is already an array $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; $repeated_tag_index[$tag.'_'.$level]++; } else {//This section will make the value an array if multiple tags with the same name appear together $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array $repeated_tag_index[$tag.'_'.$level] = 2; if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well $current[$tag]['0_attr'] = $current[$tag.'_attr']; unset($current[$tag.'_attr']); } } $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; $current = &$current[$tag][$last_item_index]; } } elseif($type == "complete") { //Tags that ends in 1 line '<tag />' //See if the key is already taken. if(!isset($current[$tag])) { //New Key $current[$tag] = $result; $repeated_tag_index[$tag.'_'.$level] = 1; if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; } else { //If taken, put all things inside a list(array) if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array... // ...push the new element into that array. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; if($priority == 'tag' and $get_attributes and $attributes_data) { $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; } $repeated_tag_index[$tag.'_'.$level]++; } else { //If it is not an array... $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value $repeated_tag_index[$tag.'_'.$level] = 1; if($priority == 'tag' and $get_attributes) { if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well $current[$tag]['0_attr'] = $current[$tag.'_attr']; unset($current[$tag.'_attr']); } if($attributes_data) { $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; } } $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken } } } elseif($type == 'close') { //End of tag '</tag>' $current = &$parent[$level-1]; } } return($xml_array); } ?> The strange thing is, this function seems to be used a lot, I even started using it. The issue however, is memory. When the xml is anymore than a small size, it causes out of memory errors while trying to use this function. Does anyone have a similar alternative that is better on the memory? Hi: Is this the correct way to do a memory form for a checkbox? Code: [Select] <input name="status[]" id="pending" value="<?=$_POST['pending'] ?>" type="checkbox" /> Hi Guys, Been a while since I've been on the forums but I have a really bugging problem that I've never seen before. I have two websites one of which is http://www.get-xbox-kinect.co.uk which I have used the same php script for. The script fetches a link from the database when executed by the user clicking a external link. I have had reports from users saying that when they hit a link the external page loads up but at the same time an alert window appears saying 'not enough memory at line xx'. For some reason it seems to be a different line number for different users too. I can only confirm this happens in IE8 but I guess it will probably do the same thing in IE6 and 7 too. It is not an issue however in Firefox or Safari. Here is my exit link script Code: [Select] <?php include('./globals.php'); $affiliate = $_GET['affil']; $user_ip = $_SESSION['user_ip']; date_default_timezone_set('Europe/London'); $date = date('D, d M Y H:i:s'); $entry_url = $_SESSION['referer']; $result = mysql_query("SELECT url FROM affiliate WHERE affiliate = '". $affiliate ."'") or die(mysql_error()); $row = mysql_fetch_array($result); $exitlink = $row[0]; if($user_ip == '00.00.000.00') { header("Location: " . $row["url"] . ""); } else { $result = mysql_query("UPDATE affiliate SET clicks = clicks +1 WHERE affiliate ='$affiliate'") or die(mysql_error()); mysql_query("INSERT INTO affiliate_clicks (affiliate, user_ip, date, entry_url) VALUES('$affiliate', '$user_ip', '$date', '$entry_url') ")or die(mysql_error()); header("Location: " . $row["url"] . ""); }; ?> Not sure if it actually has anything to do with this script or it's something else at play. I'm sure someone must have come across this problem before. Any help would be much appreciated! Thanks. Hi all, When I run the script from browser, it loads fine but when I want it to run from cronjob, it gives memory exhausted error each time cron runs. What do you think about this? What are your solutions? Here is the error: "Allowed memory size of 67108864 bytes exhausted (tried to allocate 5307694 bytes) in ...." Here are the server limits: max_execution_time = 7200 max_input_time = 600 memory_limit = 1024M Thanks. I have a PHP script, that should be simple ... it just reads some filenames from a CSV file and copies the associated files to a location, but due to my lack of skill it is HUNGRY! This sucker eats like 400M of RAM and then dies. What am I doing wrong? This seems like a simple thing .. Here's the code: <?php ignore_user_abort(true); /** * Tell WordPress we are doing the CRON task. * * @var bool */ if (!defined('ABSPATH')) { /** Set up WordPress environment */ require_once('./wp-load.php'); } if (count($argv) < 2) die(); $filename = $argv[1]; $filenamearr = explode('.', $filename); $extensionarr = array('csv', 'CSV'); if (in_array($filenamearr[count($filenamearr) - 1], $extensionarr)) { $destination_path = ABSPATH . "vielerets/data/"; if (!file_exists($destination_path)) { mkdir($destination_path, 0777); } $target_path = $destination_path . $filename; $csv_target_path = $target_path; $fd = fopen ($target_path, "rt"); if (!$fd) die(); if (!feof ($fd)) $buffer = fgetcsv($fd, 4096); $rowcount = 0; $last_postid = 0; $pre = -1; while (!feof ($fd)) { try{ $buffer = fgetcsv($fd, 4096); if (count($buffer) < 4) continue; $rowcount++; if ($pre != $buffer[0]) { $pre = $buffer[0]; //The Query query_posts('meta_key=sysid&meta_value='.$buffer[0]); //The Loop if ( have_posts() ) { the_post(); $last_postid = get_the_ID(); echo "update property images: $last_postid\n"; wp_reset_query(); } else { wp_reset_query(); continue; } } $menu_order = 0; $image_folder_name = 'property/'; if($buffer[3]) { $image_name = $buffer[3]; $menu_order = $buffer[1]; $image_name_arr = explode('/',$image_name); $img_name = $image_name_arr[count($image_name_arr)-1]; $img_name_arr = explode('.',$img_name); $post_img = array(); $post_img['post_title'] = $img_name_arr[0]; $post_img['post_status'] = 'attachment'; $post_img['post_parent'] = $last_postid; $post_img['post_type'] = 'attachment'; $post_img['post_mime_type'] = 'image/jpeg'; $post_img['menu_order'] = $menu_order; $last_postimage_id = wp_insert_post( $post_img ); update_post_meta($last_postimage_id, '_wp_attached_file', $image_folder_name.$image_name); $post_attach_arr = array( "width" => 580, "height" => 480, "hwstring_small"=> "height='150' width='150'", "file" => $image_folder_name.$image_name, ); wp_update_attachment_metadata( $last_postimage_id, $post_attach_arr ); unset($post_img); unset($post_attach_arr); echo " add images: $img_name\n"; } unset($buffer); } catch (Exception $ex) { echo "$rowcount\n"; } } echo "image csv uploaded successfully\n"; echo "Total of $rowcount records inserted\n"; } ?> This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=332709.0 // in this case, $teams_count = 16 for($j = $tournament->teams_count; $j != 1; $j/2) { echo $j; } What I expect: 16 | 8 | 4 | 2 | 1 What I get: memory exhaustion. Code: [Select] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 128716801 bytes) Is it so late, I'm missing something? In the following code the memory usage balloons up to 500MB
while ($priceRow = $pricestimes->fetch_assoc()) { //if another product if ($last_product_id != $priceRow['product_id']){ // 100 products together if ($count % 100 == 0) { echo 'MEM AFTER 100 products- ' . (memory_get_usage(true) / 1024 ). PHP_EOL; //add_price_array_to_db($pricesArr) ; $pricesArr = array(); } $count++; $last_product_id = $priceRow['product_id'] ; } $pricesArr[$priceRow['product_id']][$priceRow['insertion_timestamp']] = $priceRow['price']; }The 'echo MEM AFTER 100 products-' line shows that the memory of the script keeps on increasing. Any ideas why there is a memory leak here ? The $pricestimes is a huge mysqli resultset with 23 million rows, so I suspect that may be the issue here. Unless there is something wrong with the script logic. btw the 'add_price_array_to_db($pricesArr)' is uncommented in the real code. But even with this line commented out the memory leak is there. Thanks Edited by nik_jain, 17 August 2014 - 01:30 PM. 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; } } /** Hello, So I am trying to reduce my memory footprint to the lowest possible value without sacrificing functionality. I noticed that if I write a comment that is 2kb long, it costs me 2kb more memory to run the script. How can I get PHP to free the memory taken up by comments? It seems like it would be a real pain in the butt to have to run php -w on all my source files each time I update my server. Any ideas? Thanks. David 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 Hi. I am working with some rather large files. Sometimes I am just opening a file and trying to find an entry on a single line. Sometimes I am opening a file and working on each line.
Whats the most efficient way to work with a large file?
If I open a file, and place all lines into an array, I will use a lot of memory.
If I open a file, and work on it line by line, I should use much less memory, but will the process take longer?
Whats most efficient?
I have underlined my main question above.
Thanks :-)
|