JSONP 개발을 처음 접했고 IE 7/8이 JSONP 스크립트에 필요한 메모리를 정리하지 않는다는 것을 알았습니다. 이것은 몇 시간을 실행 한 후 내 페이지에서 매우 높은 메모리 소비를 초래합니다.인터넷 탐색기에서 JSONP 메모리를 정리하는 방법
인터넷을 둘러 보았을 때 대부분의 픽스는 Neil Fraser의 팁을 기반으로합니다. 블로그에서 그것은 당신이 불행하게도 삭제가 IE에서 "개체이 작업을 지원하지 않습니다"의 오류를 생성합니다
var tmp;
while (tmp = document.getElementById('JSONP')) {
tmp.parentNode.removeChild(tmp);
// this deletion will create error in IE.
for (var prop in tmp) delete tmp[prop];
tmp = null;
}
과 같은 코드를 사용하여 스크립트의 모든 속성을 삭제할 필요가 있다고하고는 무료로하지 않습니다 기억.
내 질문은 내 JSONP 스크립트의 메모리를 실제로 비우는 방법입니다. 당신은 두 개의 파일에 코드를 붙여 메모리 누수를 다시 만들 수 있습니다
Testing.html
<html><head></head><body><script>
var script,
head = document.getElementsByTagName('head')[0],
loadCount= 0,
uuid= 1,
URL= "memleaktest.js?uuid=",
clearConsole = function() {
var con= document.getElementById("console");
while (con.childNodes.length)
con.removeChild(con.childNodes[0]);
},
log = function(msg) {
var div= document.createElement("DIV"),
text= document.createTextNode(msg),
con= document.getElementById("console");
div.appendChild(text);
con.appendChild(div);
},
test = { "msg" : null, "data" : null };
var loaded= function() {
if (!test.msg)
return setTimeout(loaded,10);
log("loaded #" + loadCount + ": " + test.msg);
var tmp;
while (tmp = document.getElementById('JSONP')) {
tmp.parentNode.removeChild(tmp);
// not working in IE 7/8
// for (var prop in tmp) delete tmp[prop];
tmp = null;
}
test.msg = test.data = null;
if (--loadCount)
setTimeout(load, 100);
};
var load = function(){
var url= URL + (uuid ++);
log("load via JSONP: "+url);
script= document.createElement('script');
script.id = 'JSONP';
script.type = 'text/javascript';
script.charset = 'utf-8';
script.src = url;
head.appendChild(script);
setTimeout(loaded,1000);
};
</script>
<div>
<button onclick="loadCount=3; load();" name="asd" value="asdas">jsonp load</button>
<button onclick="clearConsole();" name="asd" value="asdas">clear Console</button>
</div>
<div id="console"></div>
</body></html>
memoryleaktest.js
test.msg = "loaded #"+loadCount;
test.data = "test data with 1MB size";
:
나는 다음과 같이 내 테스트 코드를 삽입 Testing.html을 엽니 다.
누수를 추적하기 위해 Drip을 사용했으며 Drip에서 메모리가 계속 증가하고 "<"스크립트 ">"가 삭제되지 않음을 알 수 있습니다.
도움을 주셔서 감사합니다.
위대한 첫 번째 질문입니다. 에 오신 것을 환영합니다. –