Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario:
Open a new XmlHttpRequest and request a large amount of data through a php file.

Issue:
Memory leaks.


JavaScript
function GetNewData()
{
	temptimestamp = Number(new Date());
	var nuUrl = '../Ref.php' + '?ReadData=' + FileName + '&temptime=' + temptimestamp;

	if(window.XMLHttpRequest)
	{
		var xhrCheckRequestnew = new XMLHttpRequest();
		xhrCheckRequestnew.abort();
		xhrCheckRequestnew.onreadystatechange = function () {
			if (xhrCheckRequestnew.readyState == 4)
			{
				if (xhrCheckRequestnew.status == 200)
				{
					var response = "";
					response = xhrCheckRequestnew.responseText;
					if (response != "")
					{
						var readValues = response.split(',');
						UpdateCurrentPage(readValues);
						readValues = null;
						delete readValues;
						response = null;
						delete response;
					}
					response = null;
					delete response;
					xhrCheckRequestnew = null;
					delete xhrCheckRequestnew;
					temptimestamp = null;
					delete temptimestamp;
					setTimeout(GetNewData, 1000);
				}
			}
		}
		xhrCheckRequestnew.open("GET", nuUrl, false);
		xhrCheckRequestnew.timeout = 4000;
		xhrCheckRequestnew.ontimeout = function () {}
		xhrCheckRequestnew.send();
	}
	else if(window.ActiveXObject)
	{
		var xhrCheckRequestnew = new ActiveXObject("Microsoft.XMLHTTP");
		if(xhrCheckRequestnew)
		{
			xhrCheckRequestnew.abort();
			xhrCheckRequestnew.onreadystatechange = function () {
				if (xhrCheckRequestnew.readyState == 4)
				{
					if (xhrCheckRequestnew.status == 200)
					{
						var response = "";
						response = xhrCheckRequestnew.responseText;
						if (response != "") 
						{
							var readValues = response.split(',');
							UpdateCurrentPage(readValues);
							readValues = null;
							delete readValues;
							response = null;
							delete response;
						}
						response = null;
						delete response;
						xhrCheckRequestnew = null;
						delete xhrCheckRequestnew;
						temptimestamp = null;
						delete temptimestamp;
						setTimeout(GetNewData, 1000);
					}
				}
			}
			xhrCheckRequestnew.open("GET", nuUrl, false);
			xhrCheckRequestnew.timeout = 4000;
			xhrCheckRequestnew.ontimeout = function () {}
			xhrCheckRequestnew.send();
		}
	}
}


I am having huge memory leaks, i am setting variables to null and deleting them, i dont understand where the memory leak is coming from. What should i do to avoid the leaks?
Posted
Comments
ZurdoDev 21-May-13 13:18pm    
How do you know you are getting memory leaks?
amarasat 21-May-13 13:53pm    
I am looking at the taskmessenger for firefox.exe or any other browser.
Zoltán Zörgő 21-May-13 13:36pm    
- What OS, what browser?
- Supposing, that object deletion helps, you could optimize it considerably, with reordering like this:

var readValues = response.split(',');
response = null;
delete response;
UpdateCurrentPage(readValues);
readValues = null;
delete readValues;

instead of:

var readValues = response.split(',');
UpdateCurrentPage(readValues);
readValues = null;
delete readValues;
response = null;
delete response;

- how large is that result anyway?
- are you sure, that UpdateCurrentPage is not wasting resources?
- do you really need to split the result before processing int in UpdateCurrentPage, and do you really need to spltit it at all?
- have you tracked resource usage of the browser?
amarasat 21-May-13 13:56pm    
I am making those changes and will update you soon.
- Result is a string with numbers from 1 to 1000 separated by ";".
-UpdateCurrentPage, i even disabled it and checked there are memory leaks.
-I need to split it, but let me try splitting in UpdateCurrentPage.
-S using task messenger.
Zoltán Zörgő 21-May-13 14:06pm    
- Again: what OS, what browser and version?
- 1000 numbers are nothing for a browser
- what is telling you that you have memory leaks?
- you have other serious problems, like using xhrCheckRequestnew after deleting it...

I don't think delete does what you think it does in JavaScript. According to Mozilla[^]:

Quote:
Unlike what common beliefs suggests, the delete operator has nothing to do with directly freeing memory
and
Quote:
delete is only effective on an object's properties. It has no effect on variable or function names.


Since you're using it on local variables, it should do nothing. Additionally, even if it did work that way, you would want to call it before setting a value to null, otherwise you are just deleting null, not the information that was there before.
 
Share this answer
 
Comments
amarasat 22-May-13 12:27pm    
I was able to solve the memeory leak in Firefox. All i did was these changes

I removed the variable "readValues" completely. I made nuUrl global variable so it is not created always. I moved the splitting of the array into the function "UpdateCurrentPage". The memory leak has been solved in FF.

However i still have the memory leak on Internet Explorer. I cannot make nuUrl global here because the time stamp is used to solve the cache issue of Internet Explorer. This is the code that is causing the issue.

var nuUrl = '../Ref.php' + '?ReadData=' + FileName + '&temptime=' + temptimestamp;

This local variable's memory is not cleaned in Internet Explorer at the time of sending xmlhttprequest. How do i clean this nuUrl memory in IE.
There it is! How long have you ran this code? Run it for hours and see if Firefox crashes. It probably won't. If you make high load on the engine, garbage collector will act only if runs out of resources. Use a tool like this to track GC activity: MemChaser[^] . I don't know what your intention was, but load and performance testing is much more complicated than starting many instances of the browser and running code infinitely.
 
Share this answer
 
Comments
amarasat 22-May-13 12:27pm    
I was able to solve the memeory leak in Firefox. All i did was these changes

I removed the variable "readValues" completely. I made nuUrl global variable so it is not created always. I moved the splitting of the array into the function "UpdateCurrentPage". The memory leak has been solved in FF.

However i still have the memory leak on Internet Explorer. I cannot make nuUrl global here because the time stamp is used to solve the cache issue of Internet Explorer. This is the code that is causing the issue.

var nuUrl = '../Ref.php' + '?ReadData=' + FileName + '&temptime=' + temptimestamp;

This local variable's memory is not cleaned in Internet Explorer at the time of sending xmlhttprequest. How do i clean this nuUrl memory in IE.
Zoltán Zörgő 22-May-13 14:37pm    
You can simply make this variable global. Just declare it globally and change it's value at it's current place.
I still believe there is no memory leak, you simply optimized resource consumption of your code - which is good.
amarasat 23-May-13 11:11am    
Unfortunately there is a memory leak. Most of it has been solved. I left an IE page open for 20 hours and the memory usage of IE has increased by 10 mb within 20 hours. I just have one question.

var xhrCheckRequestnew = new XMLHttpRequest(); and
var xhrCheckRequestnew = new ActiveXObject("Microsoft.XMLHTTP"); are two new objects created every one second.

xhrCheckRequestnew.open("GET", nuUrl, true); is an asynchronous operation, do you think the two new object are getting deleted. This is where the memory is not freed i guess. How do i delete or free xhrCheckRequestnew object in an asynchronous operation?
Zoltán Zörgő 23-May-13 12:47pm    
Believe, what you want. The case is that the GC has simply not recovered the memory in that 20 hours, because it considered those 10mb an acceptable working set. Not the increase, but the increase rate and it's tendency is important. All major browsers use different JS engines, they have different GC profile as well. There are tools you could use to really monitor resource usage of the browser.
Still, resue such objects if you can. And read this to make a more GC friendly code: http://buildnewgames.com/garbage-collector-friendly-code/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900