内存泄漏JavaScript,无法解决它。 [英] Memory leak JavaScript, unable to solve it.

查看:69
本文介绍了内存泄漏JavaScript,无法解决它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

打开一个新的XmlHttpRequest并通过php文件请求大量数据。



问题:

内存泄漏。





Scenario:
Open a new XmlHttpRequest and request a large amount of data through a php file.

Issue:
Memory leaks.


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();
		}
	}
}





我有巨大的内存泄漏,我设置变量null并删除它们,我不明白内存泄漏的来源。我该怎么做才能避免泄密?



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?

推荐答案

我不认为删除做你的想法它在JavaScript中。根据 Mozilla [ ^ ]:



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

引用:

与常见的信念不同,删除操作符与直接释放内存

Unlike what common beliefs suggests, the delete operator has nothing to do with directly freeing memory

引用:

删除仅对对象的属性有效。它对变量或函数名称没有影响。

delete is only effective on an object''s properties. It has no effect on variable or function names.





因为你在局部变量上使用它,所以它什么都不做。另外,即使它确实以这种方式工作,你也可以在之前将其设置为将值设置为 null ,否则你只是删除 null ,而不是以前的信息。



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.


就在那里!你运行这段代码多久了?运行几个小时,看看Firefox是否崩溃。它可能不会。如果您对引擎进行高负载,垃圾收集器将仅在资源耗尽时执行操作。使用此类工具跟踪GC活动: MemChaser [ ^ ]。我不知道你的意图是什么,但负载和性能测试要比启动浏览器的许多实例和无限运行代码复杂得多。
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.


这篇关于内存泄漏JavaScript,无法解决它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆