Jump to content

Memory Leaks?


cmazur

Recommended Posts

Hey All,

 

I've got some code that constantly updates a table on my web page. There is an update every 250 milliseconds.

The problem is that my memory usage for IE is outrageous after about 5 minutes of running (roughly 600 mb of memory used).

 

I'm not sure if this is a problem with my PHP script that pulls the data or the use of Sajax.

 

My code is below:

 

PHP Script to pull data:

 

<?php
require("Sajax.php");
session_start();

//Execute a SQL query and return a row
function execute_mysql($sql)
{
	//Connect to Database
	$db_host = "host";
	$db_user = "username";
	$db_pass = "password";
	$db_name = "table";

	$link = mysql_connect( $db_host, $db_user, $db_pass );

	if(!$link)
	{
		unset($link);
		return -1;	//database connection failed
	}
	else
	{
		$db_selected = mysql_select_db( $db_name );
		if(!$db_selected)
		{
			mysql_close($link);
			unset($link);			
			unset($db_selected);
			return -1;	//database not selected
		}


		$res = mysql_query( $sql );
		if(!$res)
		{
			mysql_close($link);
			unset($link);
			unset($db_selected);
			unset($res);
			return -1;	//bad query
		}

		if(mysql_num_rows($res) == 0)
		{
			mysql_close($link);
			unset($link);
			unset($db_selected);
			unset($res);
			return -1;	//empty query results
		}

		$row = mysql_fetch_assoc($res); 

		mysql_close($link);
		unset($link);
		unset($db_selected);
		unset($res);

		return $row;
	}
}

//All 12 of the functions to be exported are in this format 
            // They just pull different data

function f1()
{
	$sql = "SELECT STATEMENT;";
	$row = execute_mysql($sql);
	echo "$row['col2']";

	unset($sql);
	unset($row);
}

//Setup the Ajax
sajax_init();

sajax_export("f1");
sajax_export("f2");
sajax_export("f3");		
sajax_export("f4");
sajax_export("f5");		
sajax_export("f6");
sajax_export("f7");		
sajax_export("f8");
sajax_export("f9");		
sajax_export("f10");
sajax_export("f11");		
sajax_export("f12");				

sajax_handle_client_request();
?>

 

The code the contains the Javascript for the calling of the functions and the timer event

 


<script type='text/javascript'>

<?php
	sajax_show_javascript();
?> 

var secs;
var timerID = null;
var timerRunning = false;
var delay = 250;

function InitializeTimer()
{
	secs = 1;
	StopTheClock();
	StartTheTimer();
}

function StopTheClock()
{
	if(timerRunning)
		clearTimeout(timerID);
	timerRunning = false;
	secs = 1;
}

function StartTheTimer()
{
	if(secs == 1)
	{
		<?php
			//Set the initial values 				f1();
			f2();
			f3();
			f4();
		?>

	}

	//document.getElementById("divMem").innerHTML = "<?php echo memory_get_usage(false); ?>";

	//self.status = secs + " Updates Performed";
	secs = secs + 1;
	timerRunning = true;

	x_f1(f1_cb);
	x_f2(f2_cb);

	x_f3(f3_cb);
	x_f4(f4_cb);	

	x_f5(f5_cb);
	x_f6(f6_cb);

	x_f7(f7_cb);
	x_f8(f8_cb);

	x_f9(f9_cb);
	x_f10(f10_cb);

	x_f11(f11_cb);
	x_f12(f12_cb);

	timerID = self.setTimeout("StartTheTimer()", delay);
}

function f1_cb(f1)
{
	var temp = f1;
	if(temp != -1)	
		document.getElementById("lblF1").innerHTML = temp;
}

Link to comment
Share on other sites

This may not be the cause of that memory usage, but why are you connecting to your database in your execute_mysql() statement? It'd be better if you connected outside of that function so you don't have to write as much for that function - in fact, there's not much use for it.

 

As for Sajax and all that, I'm not sure, since I haven't worked with it much. But why are you using a timer to execute ajax requests? Could that possibly overlap? Plus, why do you define InitializeTimer() with secs=1, in which you call StartTheTimer(), which then checks if secs==1, where you could just move that code to InitializeTimer() without the conditional in it.

 

Do you know what happens when you try a different browser?

Link to comment
Share on other sites

This may not be the cause of that memory usage, but why are you connecting to your database in your execute_mysql() statement? It'd be better if you connected outside of that function so you don't have to write as much for that function - in fact, there's not much use for it.

 

As for Sajax and all that, I'm not sure, since I haven't worked with it much. But why are you using a timer to execute ajax requests? Could that possibly overlap? Plus, why do you define InitializeTimer() with secs=1, in which you call StartTheTimer(), which then checks if secs==1, where you could just move that code to InitializeTimer() without the conditional in it.

 

Do you know what happens when you try a different browser?

 

Thanks, I'll take into consideration the suggestions you've made.

 

Any other thoughts?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.