关于Javascript的内存泄漏问题(3)
作者:海啸 来源:海啸博客 发布时间:2008-04-15 07:46:00
标签:内存,泄露,javascript
http://talideon.com/weblog/2005/03/js-memory-leaks.cfm 一文中的方法类似:
/*
* EventManager.js
* by Keith Gaughan
*
* This allows event handlers to be registered unobtrusively, and cleans
* them up on unload to prevent memory leaks.
*
* Copyright (c) Keith Gaughan, 2005.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* (CPL) which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/cpl.php
*
* This software is covered by a modified version of the Common Public License
* (CPL), where Keith Gaughan is the Agreement Steward, and the licensing
* agreement is covered by the laws of the Republic of Ireland.
*/
// For implementations that don't include the push() methods for arrays.
if(!Array.prototype.push){
Array.prototype.push=function(elem){
this[this.length]=elem;
}
}
var EventManager={
_registry: null,
Initialise: function(){
if(this._registry==null){
this._registry=[];
// Register the cleanup handler on page unload.
EventManager.Add(window,"unload" ,this.CleanUp);
}
},
/*
* Registers an event and handler with the manager.
*
* @param obj Object handler will be attached to.
* @param type Name of event handler responds to.
* @param fn Handler function.
* @param useCapture Use event capture. False by default.
* If you don't understand this, ignore it.
*
* @return True if handler registered, else false.
*/
Add: function(obj, type, fn, useCapture){
this.Initialise();
// If a string was passed in, it's an id.
if(typeof obj=="string"){
obj = document.getElementById(obj);
}
if(obj==null || fn==null){
return false ;
}
// Mozilla/W3C listeners?
if(obj.addEventListener){
obj.addEventListener(type, fn, useCapture);
this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
return true ;
}
// IE-style listeners?
if(obj.attachEvent && obj.attachEvent("on" + type,fn)){
this._registry.push({obj: obj, type: type, fn: fn, useCapture: false });
return true ;
}
return false ;
},
/* *
* Cleans up all the registered event handlers.
*/
CleanUp: function(){
for(var i=0;i<EventManager._registry.length;i++){
with(EventManager._registry[i]) {
// Mozilla/W3C listeners?
if(obj.removeEventListener) {
obj.removeEventListener(type, fn, useCapture);
}
else if(obj.detachEvent){// IE-style listeners?
obj.detachEvent("on"+type,fn);
}
}
}
// Kill off the registry itself to get rid of the last remaining
// references.
EventManager._registry = null ;
}
};
使用起来也很简单:
<html>
<head>
<script type=text/javascript src=EventManager.js></script>
<script type=text/javascript>
function onLoad(){
EventManager.Add(document.getElementById(testCase),click,hit);
return true;
}
function hit(evt) {
alert(click);
}
</script>
</head>
<body onload='javascript: onLoad();'>
<div id='testCase' style='width:100%; height: 100%; background-color: yellow;'>
<h1>Click me!</h1>
</div>
</body>
</html>
google map api同样提供了一个类似的函数用在页面的unload事件中,解决Closure带来的内存泄露问题。
当然,如果你不嫌麻烦,你也可以为每个和native object有关的就阿vascript object编写一个destoryMemory函数,用来手动调用,从而手动解除Dom对象的事件绑定。
还有一种就是不要那么OO,抛弃Dom的一些特性,用innerHTML代替appendChild,避开循环引用。详细见http://birdshome.cnblogs.com/archive/2005/02/16/104967.html中的讨论贴。


猜你喜欢
- 本文针对开发项目中遇到的问题,进行了汇总问题1:如何选择select的option里面的值? 首先会用到一个方法 onchange();这个
- 前言Matplotlib的可以把很多张图画到一个显示界面,在作对比分析的时候非常有用。对应的有plt的subplot和figure的add_
- Python是当今日趋流行的一种脚本语言,它比Java更简单,比php更强大,并且还适用于做桌面应用的开发,在ubuntu中,更加是必须的一
- 本文实例总结了Python操作redis方法。分享给大家供大家参考,具体如下:python连接方式可参考:https://www.jb51.
- @keyup.enter失效问题情况一(我遇到的情况)@keyup.enter外部存在form表单,并且form表单里只有一个input原因
- 一个懒加载的树状表格实例实现一个树状表格,需要用到vxe-table这个库,虽然element-ui也能实现,但这个库是专门针对表格做了更多
- 一、通用爬虫通用网络爬虫是搜索引擎抓取系统(Baidu、Google、Sogou等)的一个重要组成部分。主要目的是将互联网上的网页下载到本地
- 概要:本文主要描述XHTML中相对定位和绝对定位各自的本质、用法、区别和两者之间的关系。以及使用CSS的Left、Right、Top、Bot
- Django提供了一个新的类来帮助管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表、元组或其它可迭
- 前言本文代码基于 python3.6 和 pygame1.9.4。俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一
- 开发微信小程序过程中,有个需求需要用到日期时间筛选器,查看微信官方文档后,发现官方文档的picker筛选器只能单独支持日期或者是时间,所以为
- 相信大家对python-docx这个常用的操作docx文档的库都不陌生,它支持以内联形状(Inline Shape)的形式插入图片,即图片和
- 在对于python中的装饰器,我们一般会使用它辅助方法。在我们学习的上下文管理器中,有一个@contextmanager装饰器,它能够帮助我
- 开发环境:python版本2.X#!/usr/bin/env python# -*- coding:utf-8 -*-# 适合python版
- 处理办法,删除该文件,或清空该文件内容;我的处理是清空后,再设置该文件权限为Everyone拒绝访问。
- 前言前几天去我姐家里蹭饭吃,发现我姐家里的小外甥女儿已经上小学了,正在疯狂赶作业,加减乘除还有很多题目都经常出错。我姐说老师每天给他们布置了
- 本文通过实例为大家分享了python实现批量提取指定文件夹下同类型文件,供大家参考,具体内容如下代码import osimport shut
- 一、了解FTP服务器FTP(文件传输协议),运行在tcp洗衣上,使用两个端口,即数据端口和命令端口,也称之为控制端口。默认情况下,20是数据
- 做查询页面,查询条件比较多的时候往往会涉及到级联。举个简单的例子,拿教务系统来说,我们要查询教学计划信息,查询条件是入学批次、学生层次(专升
- //获得视频文件的缩略图function getVideoCover($file,$time,$name) { &nb