Jump to content

Need Memory Leak Help


asianmike
 Share

Recommended Posts

I have a php page that is leaking pretty bad. I'm not sure if this belongs in this forum or the php one but I don't think it's the php that giving me problems. I've tried to use tools like drip and sieve, and even just the task manager, to help narrow the problem down, but I'm not exactly sure how to fix it. I've tried nulling and deleting variables to clean stuff up but it's still not helping. Can someone give me a hand at cleaning my code up? I know that jquery has a tendency to leak, but I'm hoping that there are some other things in the code that can be straightened out, hopefully something blatently wrong. I'm fairly new at the mem leak stuff. Thanks. Sorry for the large amount of code. :)

<?phpinclude_once $_SERVER["DOCUMENT_ROOT"] . "/include/header.php";include_once $_SERVER["DOCUMENT_ROOT"] . "/include/dbfunctions.php";header("Cache-Control: no-cache"); //needed because IE likes to cache stuff?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta http-equiv="refresh" content="3600"><html xmlns:"http://www.w3.org/1999/xhtml"><head><? include_once $_SERVER["DOCUMENT_ROOT"] . "/include/scripts.php"; ?>   <script type="text/javascript" src="/scripts/sortabletables.js"></script><script type="text/javascript" src="/scripts/reverse.js"></script><script type="text/javascript">var updateTopObj;var day = $.getUrlParameter("day");var page = $.getUrlParameter("page");$(document).ready(function(){		     updateTopObj = new UpdatablePage({		   updateFunction: updateTop,		   interval: 4000,		   dataType: "xml",		   url: "/resources/Events.xml"  });									  	    updateTopObj.update();  updateTopObj.start();   $('#EventsTable').styleTable({});  $('.table-sortable').click(function(){	$('tr','#EventsTable').removeClass("odd").removeClass("even");	$('#EventsTable').styleTable({});	parent.OMC.pause(frameId);  });}); // end $(document).ready function updateTop(xml){  var $xml = $(xml);  var $root = $('eventsInfoTable', $xml);  var $numDays = $('eventRec', $root).length; // alert($numDays);    var $dayMenuTable = $('#DayMenuTable');  var $dayMenuBody = $('tbody',$dayMenuTable).empty();  var $newDayRow = $('<tr></tr>').appendTo($dayMenuBody);    var $date_xml = $('date',$root);  var $index_xml = $('dayIndex', $root);  var $pages_xml = $('numPages', $root);  var $records_xml = $('numRecords', $root);  var date_arr = [];  var index_arr = [];  var page_arr = [];  var records_arr = [];    $('eventRec',$root).each(function(i){	$date_xml = $('date',this).text().substring(0,5); // only show dd/mm	date_arr[i] = $date_xml; 	$index_xml = $('dayIndex',this).text();	index_arr[i] = $index_xml; 	$page_xml = $('numPages',this).text();	page_arr[i] = $page_xml; 	$records_xml = $('numRecords',this).text();	records_arr[i] = $records_xml;   });  for(i=0; i<date_arr.length; i++){	if(date_arr[i] != "00/00"){	  $dayParam = index_arr[i];	}  }  if(!day){ day = $dayParam;}  for(i=date_arr.length-1; i>=0; i--){	if(date_arr[i] != "00/00") { // don't display 00/00 dates	  $topPageNum = parseInt(page_arr[i])-1;	  //alert(index_arr[i]+" : "+day);	  if(index_arr[i] == day){ 		if(!page){page = page_arr[i]-1;}		$cell = $('<td width=45><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + $topPageNum + '" class="submenuSelected" onclick="parent.OMC.play(frameId);">' + date_arr[i] + '</a></td>').appendTo($newDayRow);		$('#dayTitle').empty().append(date_arr[i]+' Events Log');	  } else $cell = $('<td width=45><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + $topPageNum + '"  class="submenuNormal" onclick="parent.OMC.play(frameId);">' + date_arr[i] + '</a></td>').appendTo($newDayRow);	} //end if date != 00/00  }    //alert("DAY"+day+"_PG"+page);  $cell = $('<td><form action="CSV.php" target="_blank" method="GET"><input type="submit" class="genCSVLink" value="View Weekly CSV"/></form></td>').appendTo($newDayRow);    var $pageMenuTable = $('#PageMenuTable');  var $pageMenuBody = $('tbody',$pageMenuTable).empty();  var $newPageRow = $('<tr></tr>').appendTo($pageMenuBody);    for(i=date_arr.length-1; i>=0; i--){	  if(index_arr[i] == day){ 		$totalPages = parseInt(page_arr[i]);		$pageInc = 20;		$totalInc = parseInt(($totalPages-1)/$pageInc)+1; // $totalPages -1 for incase there are 20 pages exactly		//$currentPage = parseInt(page_arr[i])-parseInt(page);		$currentPage = parseInt(page)+1;		$currentInc = parseInt(($currentPage-1)/$pageInc) + 1; // $currentPages -1 for incase there are 20 pages exactly		if($currentInc != 1){		  $prevPage = ($currentInc-1)*$pageInc-1;		  $cell = $('<td width=20><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + $prevPage + '" class="submenuNormal" onclick="parent.OMC.play(frameId);">Prev</a></td>').appendTo($newPageRow);		}		for(j=0; j<=page_arr[i]-1; j++){		  $pageNum = parseInt(j)+1;		  //$pageNum = $totalPages - parseInt(j);		  if(($pageNum >= ($currentInc*$pageInc)-$pageInc+1) && ($pageNum <= ($currentInc*$pageInc))){			if($pageNum == $currentPage){			  $cell = $('<td width=20><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + j + '" class="submenuSelected" onclick="parent.OMC.play(frameId);">' + $pageNum + '</a></td>').appendTo($newPageRow);			} else{			  $cell = $('<td width=20><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + j + '" class="submenuNormal" onclick="parent.OMC.play(frameId);">' + $pageNum + '</a></td>').appendTo($newPageRow);  			  			}		  }  		}		if($currentInc != $totalInc){		  $nextPage = ($currentInc*$pageInc);		  $cell = $('<td width=20><a href="/webpage/events.php?day=' + index_arr[i] + '&page=' + $nextPage + '" class="submenuNormal" onclick="parent.OMC.play(frameId);">Next</a></td>').appendTo($newPageRow);		}		if($currentPage == $totalPages){		  if(records_arr[i] == 0){			var $startRecord = 0;			var $endRecord = 0; 		  } else{			var $startRecord = ($currentPage-1)*100+1;			var $endRecord = records_arr[i];		  }		} else if($totalPages == 0){		  var $startRecord = 0;		  var $endRecord = 0;		}		else{		  var $startRecord = ($currentPage-1)*100+1;		  var $endRecord = ($currentPage)*100;		}		$('#RecordsMenuTable').empty().append('Displaying records ' + $startRecord + ' to ' + $endRecord + ' of ' + records_arr[i]);	  }  }	  // make events table  $.ajax({ 	 type: "GET",	 url: "/resources/DAY"+day+"_PG"+page+".xml",	 dataType: "xml",	 success: function(xml){		 var $eventsXml = $(xml);		 var $eventsRoot = $('eventsTable', $eventsXml);		 var eventsBody = document.getElementById('EventsBody');		 var numNewRows = $('eventRec', $eventsRoot).length;		 var $currentRows = $('tr', $(eventsBody));		 var numRows = $currentRows.length; 		 		 $('th', $('#EventsTable')).removeClass("table-sorted-asc").removeClass("table-sorted-desc");		 var docFragment = document.createDocumentFragment();		 var newTable = false;		 //alert(numNewRows+" : "+numRows);		 //If we have more rows to append than exist, re-create the entire table		 if(numNewRows != numRows){		   $(eventsBody).empty();		   newTable = true;		   //alert("need new table");		 }		 //else{alert("don't need new table");}		 var trElem, tdElem;		 $('eventRec', $eventsRoot).reverse();		 if(newTable) {		   $('eventRec', $eventsRoot).each(function(i){			 $record = $(this);			 //alert($(this).text());			 trElem = document.createElement('tr');			 docFragment.appendChild(trElem);			 $record.children('dateTime').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('type').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('source').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('eventId').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('assAlrm').each(function(i) { 			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('username').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   tdElem.innerHTML = $(this).text();  			 });			 $record.children('desc').each(function(i) {			   tdElem = document.createElement('td');			   trElem.appendChild(tdElem);			   var cellText = $(this).text();			   $('bullet', $(this)).each(function(i){				 tempText = $(this).text();				 cellText = cellText.replace($(this).text(),"<br>•"+tempText);			   });			   tdElem.innerHTML = cellText;			 });			 eventsBody.appendChild(docFragment);		   });		 }		 if(newTable) {		   eventsBody.appendChild(docFragment);		   $('#EventsTable').styleTable({header:false, title:false});		 } else {}		 trElem = null;		 delete trElem;		 tdElem = null;		 delete tdElem;		 numNewRows = null;		 delete numNewRows;		 numRows = null;		 delete numRows;		 newTable = null;		 delete newTable;		 cellText = null;		 delete cellText;		 $eventsRoot = null;		 delete $eventsRoot;		 $eventsXml = null;		 delete $eventsXml;		 eventsBody = null;		 delete eventsBody;		 $record = null;		 delete $record;		 cellText = null;		 delete cellText;	 }  });  $xml = null;delete $xml;$root = null;delete $root;$numDays = null;delete $numDays;$dayMenuTable = null;delete $dayMenuTable;$dayMenuBody = null;delete $dayMenuBody;$newDayRow = null;delete $newDayRow;$date_xml = null;delete $date_xml;$index_xml = null;delete $index_xml;$page_xml = null;delete $page_xml;$records_xml = null;delete records_xml;date_arr = null;delete date_arr;index_arr = null;delete index_arr;page_arr = null;delete page_arr;records_arr = null;delete records_arr;dayParam = null;delete dayParam;topPageNum = null;delete topPageNum;i = null;delete i;$pageMenuTable = null;delete $pageMenuTable;$pageMenuBody = null;delete pageMenuBody;$newPageRow = null;delete newPageRow;$totalPages = null;delete $totalPages;$pageInc = null;delete $pageInc;$totalInc = null;delete $totalInc;$currentPage = null;delete $currentPage;$currentInc = null;delete $currentInc;$prevPage = null;delete $prevPage;j = null;delete j;$pageNum = null;delete $pageNum;$cell = null;delete $cell;$nextPage = null;delete $nextPage;$startRecord = null;delete $startRecord;$endRecord = null;delete $endRecord;} // end of updateTopfunction cleanup() {  delete updateTopObj;  delete day;  delete page;}</script><title>Events</title></head><body onUnload="cleanup();"><div class="whiteSubmenu"><table id="DayMenuTable" border="0"><tbody></tbody></table> </div><div class="tableSpacer"></div><div class="whiteSubmenu"><table id="PageMenuTable" border="0"><tbody></tbody></table> </div><div id="RecordsMenuTable" border="0"></div><hr><div id="dayTitle" style="color:#191970;font-size:12pt"></div><table id="EventsTable" title="" class="mainView table-autosort">   <thead>	  <tr>	  <th class="table-autosort table-sortable:default" style="cursor:pointer">Timestamp</th>	  <th class="table-autosort table-sortable:default" style="cursor:pointer">Type</th>	  <th class="table-sortable:default" style="cursor:pointer">Source</th>	  <th class="table-sortable:default" style="cursor:pointer">ID</th>	  <th class="table-sortable:default" style="cursor:pointer">Associated Alarm</th>	  <th class="table-sortable:default" style="cursor:pointer">Username</th>	  <th>Description</th>	  </tr>   </thead>   <tbody id="EventsBody">   </tbody></table></body></html>

Link to comment
Share on other sites

How do you know there's a memory leak? Is the page running slow? Are you getting error messages?
When I'm open the task manager or view the page in drip or sieve I can see the mem usage soar. It'll go from about 50k to about 180-200k in a matter of like 40 minutes. And then the page will start to choke up.
Link to comment
Share on other sites

When I'm open the task manager or view the page in drip or sieve I can see the mem usage soar. It'll go from about 50k to about 180-200k in a matter of like 40 minutes. And then the page will start to choke up.
It sounds like you're using Firefox. Firefox just does that and it's one thing I don't like about it, it has nothing to do with your site. Have you tested your page on different browsers?Just for your information, PHP runs on the server so it has no effect on your computer's memory usage.
Link to comment
Share on other sites

It sounds like you're using Firefox. Firefox just does that and it's one thing I don't like about it, it has nothing to do with your site. Have you tested your page on different browsers?Just for your information, PHP runs on the server so it has no effect on your computer's memory usage.
I've acutally been running it on IE7. We were having problems with it on FF. I know the code is constructed poorly. Is there anything that stands out? Like bad closures pr circular references? How would I go about fixing them? Thanks.
Link to comment
Share on other sites

You have a lot of jQuery there, which I'm not familiar with, so you'll have to see if other people can help.Putting delete statements at the end of a function is pointless, because local variables are deleted as soon as the function stops running. Setting a variable to null right before deleting it is also pointless, because deleting gets rid of the variable no matter what value it has. Which means you can get rid of all the following:

$xml = null;delete $xml;$root = null;delete $root;$numDays = null;delete $numDays;$dayMenuTable = null;delete $dayMenuTable;$dayMenuBody = null;delete $dayMenuBody;$newDayRow = null;delete $newDayRow;$date_xml = null;delete $date_xml;$index_xml = null;delete $index_xml;$page_xml = null;delete $page_xml;$records_xml = null;delete records_xml;date_arr = null;delete date_arr;index_arr = null;delete index_arr;page_arr = null;delete page_arr;records_arr = null;delete records_arr;dayParam = null;delete dayParam;topPageNum = null;delete topPageNum;i = null;delete i;$pageMenuTable = null;delete $pageMenuTable;$pageMenuBody = null;delete pageMenuBody;$newPageRow = null;delete newPageRow;$totalPages = null;delete $totalPages;$pageInc = null;delete $pageInc;$totalInc = null;delete $totalInc;$currentPage = null;delete $currentPage;$currentInc = null;delete $currentInc;$prevPage = null;delete $prevPage;j = null;delete j;$pageNum = null;delete $pageNum;$cell = null;delete $cell;$nextPage = null;delete $nextPage;$startRecord = null;delete $startRecord;$endRecord = null;delete $endRecord;

You appear to be loading XML files, if they're really large that might also be the cause for the extra memory use.

Link to comment
Share on other sites

One thing you can do is use Firebug with Firefox and click on the DOM tab, that will let you see everything that's currently defined. All of your variables, functions, and objects will be listed there. If you see anything there that you created that you don't need, find out at which point you can remove that and make sure to do so.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...