PHP - Memory Use Keeps Increasing In Loop
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. Similar TutorialsThere are two things I want done. 1. Grab the current date in TIMESTAMP form and increase by a certain number of days as assigned by $days() 2. Similar to the previous one, it will grab an already specified TIMESTAMP, and work from there All it echoes is 86400, which I assume is the number of seconds? Code: [Select] <?php $days = 1; echo strtotime("+$days days", strtotime($date)); ?> how can you increase the page number in the following line until $status returns an empty result? curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Authorization: xxxxxxxxxxxxx:xxxxxxxxxxxxxxxx", "Page: 2")); <?php require_once('upper.php'); if(isset($_POST['submit'])) { //This is code in which we can choose the type and size file uploaded or upload anything. We can declare the path of folder or can do without it. if($_FILES['uploaded_file']['size']< 200000) { /*if($_FILES['uploaded_file']['error']>0) { echo "Error occurs".$_FILES['up_test']['error']."<br/>"; } else{*/ $Title=$_POST['Title']; $City=$_POST['City']; $Content=$_POST['Content']; $Date=$_POST['Date']; echo "Uploaded Title--$Title<br/>"; echo "Uploaded City--$City<br/>"; echo "Uploaded Content--$Content<br/>"; echo "Uploaded Date of Event--$Date<br/>"; /*if(file_exists("upload/". $_FILES['uploaded_file']['name'])) { echo "File".$_FILES['uploaded_file']['name']." already exists"; } else {*/ move_uploaded_file($_FILES['uploaded_file']['tmp_name'],"upload/".$_FILES['uploaded_file']['name']); echo "File uploaded-- ".$_FILES['uploaded_file']['name']."<br><br><br>"; } else{ echo "File size is too large or Not Uploaded "; } $path="upload/".$_FILES['uploaded_file']['name']; require_once('database.php'); $result=mysqli_query($dbc,"insert into events (Title,City,Content,Photo,Date) values ('$Title','$City','$Content','$path','$Date')") or die ('Not Connected Events'); //echo "PAth"; } ?> <html> <head> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data"> <h4>Enter Events</h4><br> Enter Title <br><textarea rows="10" cols="10" name="Title"></textarea><br> Enter City <br><input type="text" name="City"><br> Enter Content <br><textarea rows="10" cols="10" name="Content"></textarea><br> <label for="file">Upload Photo<br></label> <input type="file" name="uploaded_file" /> <br /> Enter Date of Event<br> <input type="text" name="Date" value="yyyy-mm-dd"><br> <input type="submit" name="submit" value="Upload" /> </form> <?php require_once('lower.php');?> </body> </html> Hi friends............... In above code when i upload title or content within range of 20 words it works properly but when I increase numbers of words till approx. 100 words. It gives an error "Not Connected Events". Not any file is uploaded in database. I set the length of title and content in phpmyadmin is 500 and 1000 and type is varchar respectively. I can't understand where is problem???????? I think it's about something size??????? Help me Anyone??????????????? 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 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. Hello Guys, I wanted to know what is memory leakage? Where we have to takecare of ? How to avoid this thing? 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 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 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? 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 :-)
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"; } ?> 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 trying to create a login system in PHP, but I am not the greatest at PHP so I am using a source code which I found online as I found it to be more secure as it uses things like salted passwords. Anyway I am trying to add more fields to the register system so it adds them to the mysql, the source has a way to do this with arrays, but it is quite complicated so I am just using variables from the original file. There are 2 files: register.php and class.loginsys.php which contains all the functions. At first the query syntax was incorrect so I decided to use the variables created in register.php in the class.loginsys, but now it's giving me an out of memory error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 28672 bytes) in C:\xampp\htdocs\ls\class.loginsys.php on line 34 Which I am unsure of how to fix. I have tried using different variable names, checking the line, checking the whole register.php file for anything rogue. Here is the code: Top part of register.php <?php include "config.php"; ?>Config.php: <?php require "class.loginsys.php"; $LS=new LoginSystem(); ?>Then actual register part from register.php: <?php if( isset($_POST['submit']) ){ $firstname2 = $_POST['firstname']; $lastname2 = $_POST['lastname']; $user2 = $_POST['username']; $sex2 = $_POST['sex']; $country2 = $_POST['strCountryChoice']; $email2 = $_POST['email']; $pass2 = $_POST['pass']; $pass3 = $_POST['pass2']; $birthdate2 = $_POST['birthdate']; $created2 = date("Y-m-d H:i:s"); //need to add a lot more validation functions.. AKA Check if email exists and username. Password > 5 chars if( $user2=="" || $email2=="" || $pass2=='' || $pass3=='' || $firstname2=='' || $lastname2=='' || $sex2=='' || $country2=='' || $birthdate2=='' ){ echo "Fields Left Blank","Some Fields were left blank. Please fill up all fields."; exit; } if( !$LS->validEmail($email2) ){ echo "E-Mail Is Not Valid", "The E-Mail you gave is not valid"; exit; } if( !ctype_alnum($user2) ){ echo "Invalid Username", "The Username is not valid. Only ALPHANUMERIC characters are allowed and shouldn't exceed 10 characters."; exit; } if($pass2 != $pass3){ echo "Passwords Don't Match","The Passwords you entered didn't match"; exit; } $createAccount2 = $LS->register($user2, $pass2, array( "email" => $email2, "name" => $firstname2, "lastname" => $lastname2, "gender" => $sex2, "country" => $country2, "DOB" => $birthdate2, "created" => date("Y-m-d H:i:s") // Just for testing ) ); //$createAccount = $LS->register($firstname,$lastname,$user,$sex,$country,$email,$pass,$birthdate,$created); if($createAccount2 === "exists"){ echo "User Exists."; }elseif($createAccount2 === true){ echo "Success. Created account."; } } ?>And the function from the class: /* A function to register a user with passing the username, password and optionally any other additional fields. */ public function register( $id, $password, $other = array() ){ if( $this->userExists($id) && (isset($other['email']) && $this->userExists($other['email'])) ){ return "exists"; }else{ $randomSalt = $this->rand_string(20); $saltedPass = hash('sha256', "{$password}{$this->passwordSalt}{$randomSalt}"); if( count($other) == 0 ){ /* If there is no other fields mentioned, make the default query */ //old query: ("INSERT INTO `{$this->dbtable}` (`username`, `password`, `password_salt`) VALUES(:username, :password, :passwordSalt)"); //new query: ("INSERT INTO `{$this->dbtable}` (`username`, 'email' , `password`, `password_salt` , 'name' , 'lastname' , 'gender' , 'country' , 'DOB') VALUES(:username, :email, :pass, :passwordSalt, :firstname, :lastname, :gender, :country, :DOB)"); $sql = $this->dbh->prepare("INSERT INTO `{$this->dbtable}` (`username`, `password`, `password_salt`) VALUES(:username, :password, :passwordSalt)"); }else{ /* if there are other fields to add value to, make the query and bind values according to it */ //old query: ("INSERT INTO `{$this->dbtable}` (`username`, `password`, `password_salt`, $columns) VALUES(:username, :password, :passwordSalt, :$colVals)"); //new query: ("INSERT INTO `{$this->dbtable}` (`username`, 'email' , `password`, `password_salt` , 'name' , 'lastname' , 'gender' , 'country' , 'DOB') VALUES(:username, :email, :pass, :passwordSalt, :firstname, :lastname, :gender, :country, :DOB)"); $keys = array_keys($other); $columns = implode(",", $keys); $colVals = implode(",:", $keys); //l= $this->dbh->prepare("INSERT INTO `{$this->dbtable}` (`username`, `password`, `password_salt`, $columns) VALUES(:username, :password, :passwordSalt, :$colVals)"); //INSERT INTO MyGuests (firstname, lastname, email)cLUES ('John', 'Doe', 'john@example.com') $sql = $this->dbh->prepare("INSERT INTO `{$this->dbtable}` (username,email,password,password_salt,name,lastname,created,gender,country,DOB) VALUES ('$username2','$email2','$pass2','$saltedPass','$firstname2','$lastname2','$created2','$gender2','$country2','$birthdate2')"); print($sql); foreach($other as $key => $value){ $value = htmlspecialchars($value); $sql->bindValue(":$key", $value); } } /* Bind the default values */ $sql->bindValue(":username", $id); $sql->bindValue(":password", $saltedPass); $sql->bindValue(":passwordSalt", $randomSalt); $sql->execute(); return true; } }Thanks for your help. I am doing this because for a hobby I am trying to create a browser based game in which I use this login system to login the user to a main page then code all of the other pages myself. I have posted on stackoverflow and someone on their suggested that I should use a framework. If this is the case, can someone point me in the right direction? Thanks again, if you need any info ask. 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 // 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? 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" /> 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; } } /** |