PHP - How To Reduce Virtual Memory By Optimising My Php Code?
My current code (see below) uses 147MB of virtual memory! My provider has allocated 100MB by default and the process is killed once run, causing an internal error. The code is utilising curl multi and must be able to loop with more than 150 iterations whilst still minimizing the virtual memory. The code below is only set at 150 iterations and still causes the internal server error. At 90 iterations the issue does not occur.
How can I adjust my code to lower the resource use / virtual memory whilst still maintaining lightning fast speed for executing the URL and receiving the results? Alternatively, Is there an example where I am able to do overlapping HTTPS requests (rather than the below) while getting the results as they arrive. Language which supports threads to do this? Thanks! <?php function udate($format, $utimestamp = null) { if ($utimestamp === null) $utimestamp = microtime(true); $timestamp = floor($utimestamp); $milliseconds = round(($utimestamp - $timestamp) * 1000); return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp); } $url = 'https://www.testdomain.com/'; $curl_arr = array(); $master = curl_multi_init(); for($i=0; $i<150; $i++) { $curl_arr[$i] = curl_init(); curl_setopt($curl_arr[$i], CURLOPT_URL, $url); curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, FALSE); curl_multi_add_handle($master, $curl_arr[$i]); } do { curl_multi_exec($master,$running); } while($running > 0); for($i=0; $i<150; $i++) { $results = curl_multi_getcontent ($curl_arr[$i]); $results = explode("<br>", $results); echo $results[0]; echo "<br>"; echo $results[1]; echo "<br>"; echo udate('H:i:s:u'); echo "<br><br>"; usleep(100000); } ?> Similar TutorialsHi, This isn't so much a problem as my code/page works however my host shut down scripting on the site as too many connections to the database were left open apparently with this page. Also ive been looking into a way of cleaning up the code and making it less bloated but the only thing I have found is mysqli for running multiple queries in one go but I am unsure if this would help. Code: [Select] <?php session_start(); include("./includes/db_con.inc.php"); $image = $_GET["id"]; $id = str_replace("-", "/", $_GET["id"]); $cat = $_GET["cat"]; $sub_cat = $_GET["sub"]; $page_title = $cat . " - " . $sub_cat . " - " . $id; $sql_meta = mysql_query("SELECT * FROM english WHERE PRODID='$id' AND display='1'"); $row_meta = mysql_fetch_assoc($sql_meta); $meta_desc = $row_meta['META_DESC']; $meta_key = $row_meta['META_KEY']; include("./includes/head.php"); include("./includes/header.php"); ?> <div id="main-page-header"> <?php $header = "img/product-pages/product-specific-headers/" . $image . ".jpg"; if (file_exists($header)) { ?> <img src="<?php echo $img_loc; ?>/product-pages/product-specific-headers/<?php echo $image; ?>.jpg" width="940" height="310" /> <?php } else { $desc_sql = mysql_query("SELECT * FROM sub_cat WHERE name='$sub_cat'"); $row_desc = mysql_fetch_assoc($desc_sql); $description = $row_desc['description']; echo "<img src='$img_loc/product-pages/sub-cat-head/".str_replace(" ", "-", $row_desc['name'])."-sub-cat-head.jpg' width='940' height='310' />"; } ?> </div> <div id="page-content"> <div id="main-page-text"> <h1><?php $result = mysql_query("SELECT * FROM $table WHERE PRODID='$id' AND display='1'"); while($row = mysql_fetch_array($result)) { $prod_title = $row['PROD_TITLE']; echo $prod_title; ?> </h1> <h2 class="crumbs"><a href="<?php echo "$url/Products/$lang/"; ?>">Products</a> > <a href="<?php echo "$url/Category/$lang/".urlencode($cat)."/"; ?>"><?php echo $cat; ?></a> > <a href="<?php echo "$url/Range/$lang/".urlencode($cat)."/".urlencode($sub_cat)."/"; ?>"><?php echo $sub_cat; ?></a> > <?php echo $prod_title; ?> </h2> <p><?php echo $row['DESCRIPTION']; } ?></p> <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like layout="button_count" show_faces="false" width="170" font="verdana"></fb:like> <br /> <?php $i = 1; while ($i <= 8) { $filename = "img/product-pages/hovers-800px/" . $image . "-" . $i . ".jpg"; if (file_exists($filename)) { ?> <a class="fuglybox" rel="gxr" href="<?php echo "../../../../../" . $filename; ?>"> <img src="../../../../../img/product-pages/product-detail-90px/<?php echo $image . "-" . $i . ".jpg"; ?>" alt="" width="90" height="90" /></a> <?php } else { break; } $i++; } ?> <table> <th>Product Code</th> <th>Description</th> <?php $table_result = mysql_query("SELECT * FROM PRODID WHERE PRODID='$id'"); $table_entry = mysql_fetch_assoc($table_result); echo "<tr><td class='prod-id'>" . $table_entry['PROD_CODE'] . "</td>\n"; echo "<td class='prod-desc'>" . $table_entry['TA_DESC'] . "</td></tr>\n"; ?> </table> <div id="FAQ-wrap"><h2>Frequently Asked Questions</h2> <?php $QA_table = $lang . "_qanda"; if ($id=="") { $list_QA = mysql_query("SELECT * FROM $QA_table WHERE SUB_CATEGORY='$sub_cat' AND DISPLAY='1'"); } else { $list_QA = mysql_query("SELECT * FROM $QA_table WHERE PRODID='$id' AND DISPLAY='1'"); } while($row_QA = mysql_fetch_array($list_QA)) { echo "<div id='FAQ-QA'>"; echo "<h2 class='FAQ-question'>Q: ".$row_QA['QUESTION']."</h2>"; echo "<p class='FAQ-question'>A: ".$row_QA['ANSWER']."</p></div>"; } ?> <div id="FAQ-question"> <h2 class="white">Got a question about the <span class="got-question-product"><?php echo $sub_cat; ?></span> </h2> </div> </div> </div> <div id="totem-menu-container"><?php include("./includes/search.php") ?> <?php include("./includes/totem.php") ?> </div> </div> </div> <?php include("./footer.php") ?> Please note in the db_con.inc.php is the connection to the database stored in variable $con and included in the footer.php is mysql_close($con) which is also why I dont understand how connections are being left open, any help greatly appreciated. Hi guys, I have a for loop, that queries my database, sometimes, close to 240 times, I want to minmise the DB access - what would be ideal is if I can query a result set, such as this.. Code: [Select] $result = mysql_query($sql,$con); Is it possible for me to now run a query on $result. Such as Code: [Select] select * from sometable where age >= '24' So, rather than access the DB over and over again, I just query the result set, which will be much faster. Any ideas on this one guys? Cheers Zain IS there any special code for doing this? Im not familiar with this kind of optimising
Hi. I am sending out email with a php script - about 5000 at a time by send 15 or so every 20 seconds which works fine. Sometimes I need to add an attachemnt. I then find that the script will keep stalling. Is there a way to test the server qmail/mail queue to see if it isready to send another message? - rather than send it and have the script stall? It may be enough to simply be able to check that the number of messages left to be sent in the queue? Have search a lot for help on this with no results. Any server/qmail experts out there? Thanks Paul I am trying to use the GD library functions to reduce the size and ppi of images. Unfortunately my server does not support imagick, which can do this, but I have worked out a way to do the job, except that the process always makes the image 75 ppi. I would rather have some control over the pixel density. Can anyone suggest an answer please? Here is my code...... Code: [Select] <?php $filename = 'my.jpg'; $size_multiple = .5;//change to suit dimensions wanted header('Content-type: image/jpeg'); list($width, $height) = getimagesize($filename); $new_width = $width * $size_multiple; $new_height = $height * $size_multiple; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_p, null, 100); ?> hello anyone can help me to reduce image file size(reduce quality of images) using php ? Hello to everyone.
I have a php website, in which i would like to integrade a virtual keyboard in any text boxes I have on it, including in google custom search.
I found in a website a virtual keyboard which is free for integrade in javascript code.
But when I am trying to use it, It appears the text box and the keyboard together.
I would like to know if there is any way to catch and use only the virtual keyboard in the existing text boxes in my page, I would like to take only the keyboard from the set of these both text box and keyboard and use it in my site.
I don't know if I am being understandable, if no please tell me.
Thanks in advance.
The keyboard, is from the website: http://www.greywyver...script/keyboard
Thank you very much !!!
This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=347966.0 Howdy! A client's web page is .shtml and includes various PHP files via standard virtual include: Code: [Select] <!--#include virtual="session.php" --> These PHP includes worked fine until recently, when PHP files that produce no visible output began displaying a 0 (zero). For instance, the session.php file above is here in it's entirety: Code: [Select] <?php session_start(); ?> The include is on the first line of the .shtml file, and includes that bit of code, but displays a 0 in the browser. If I echo anything at all the 0 goes away, but of course we don't want to echo anything before the <html> tag in the .shtml file. The short-term and unacceptable solution is to echo an empty HTML comment, as follows: Code: [Select] <?php session_start(); echo "<!-- -->"; ?> As noted, this causes the 0 to disappear but now we have <!-- --> before the HTML. Repeat: This was not happening when the .shtml and PHP was installed, and the files have not changed. Therefore, it seems that this problem is caused by a change in PHP and/or Apache configuration. Has anyone seen this and/or can anyone explain what's going on? Thanks! Alright so I'm going to try to explain this as best I can.. Basically right now to access a specific row in a DB the url is ?id=1 The page uses the $_GET to search for the row where id=1, blah blah blah But I have another script that cannot use ?id= on url, it needs to be a static url like index.php or /dir/ or index.html, if that makes sense.. So if I had a url index.php?id=1 the script only allows index.php, thus it doesn't find the right content because without the ?id it just shows all rows, not that specific one.. So basically I need some way to make a virtual link, for example test.com/id/1 do the same as index.php?id=1 without actually creating hundreds of dir's and pages =/ Any ideas? A lot of bigger websites do this but I'm not sure how so any help would be greatly appreciated Hi, I want to remove all extra spaces and line breaks that I have in my PHP, HTML, CSS and JavaScript files, to reduce the file size, I do not want to go through 100+ files and do this manually. Is there a PHP script that would do this? Any suggestions where I should start looking? Thanks Hi all, I have a strange issue going on. It may be server related, but I don't know what to look for. I have a .php page with a few Virtual Includes: Code: [Select] <?php virtual ("nav.shtml"); ?> ...throughout it. I have also a parser that is displaying XML data in a table form. The parser works with the standard: Code: [Select] <?php include ('parser.php'); ?> ...however, if I have the Virtual above the include, the parser doesn't work. Or at least it will not "find the file" however, the file is there and it works ABOVE the virtual, displaying it fine... For example, this works: Code: [Select] <?php include ('parser.php'); ?> <?php virtual ('file.shtml'); ?> This doesn't: Code: [Select] <?php virtual ('file.shtml'); ?> <?php include ('parser.php'); ?> Any thoughts? i want to make virtual money (credits, tokens) whatever you want to call it but make no mistake this is not for profit at this point but i like the concept of visitors having something to do with other members of site for example to kill bordom and create a reason to play games, compete in competetions, it will have to conversion to real money at this point but i wish to do this script using php and mysql and have very basic coding skills in these areas anyone please help me make this script... reply and let me know if you like this idea please help me as this is achievable as fb has it and more sites are moving toward these same ideas, even tho fb is real money it is more to do with fun at this point until we do this stage another stage canot proceed it. This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=347820.0 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 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? 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 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 ?
|