GC与JS内存泄露(5)
作者:alucard 来源:Alibaba.com UED 时间:2010-09-25 19:01:00
三、Cross-Page-Leaks
仍然先看一个例子:
< html >
< head >
< script language = “ JScript “ > function LeakMemory()
{
var hostElement = document.getElementById( “ hostElement “ ); // Do it a lot, look at Task Manager for memory response
for (i = 0 ; i < 5000 ; i ++ )
{
var parentDiv =
document.createElement( “ <div onClick=’foo()’> “ );
var childDiv =
document.createElement( “ <div onClick=’foo()’> “ ); // This will leak a temporary object
parentDiv.appendChild(childDiv);
hostElement.appendChild(parentDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null ;
childDiv = null ;
}
hostElement = null ;
} function CleanMemory()
{
var hostElement = document.getElementById( “ hostElement “ ); // Do it a lot, look at Task Manager for memory response
for (i = 0 ; i < 5000 ; i ++ )
{
var parentDiv = document.createElement( “ <div onClick=’foo()’> “ );
var childDiv = document.createElement( “ <div onClick=’foo()’> “ ); // Changing the order is important, this won’t leak
hostElement.appendChild(parentDiv);
parentDiv.appendChild(childDiv);
hostElement.removeChild(parentDiv);
parentDiv.removeChild(childDiv);
parentDiv = null ;
childDiv = null ;
}
hostElement = null ;
}
</ script >
</ head > < body >
< button onclick = “ LeakMemory() “ > Memory Leaking Insert </ button >
< button onclick = “ CleanMemory() “ > Clean Insert </ button >
< div id = “ hostElement “ ></ div >
</ body >
</ html >
LeakMemory和CleanMemory这两段函数的唯一区别就在于他们的代码的循序,从代码上看,两段代码的逻辑都没有错。
标签:内存,js,javascript,IE
0
投稿
猜你喜欢
Python正则表达式教程之二:捕获篇
2023-08-24 00:27:39
基于python实现把json数据转换成Excel表格
2021-02-20 05:22:04
Window环境下MySQL UDF提权
2024-01-21 09:53:01
Vue Render函数原理及代码实例解析
2023-07-02 16:34:38
Java实现数据库连接池简易教程
2024-01-20 01:30:29
Python中装饰器的基本功能理解
2021-05-26 05:17:56
Python3查找列表中重复元素的个数的3种方法详解
2022-08-01 16:36:07
python格式化字符串的实战教程(使用占位符、format方法)
2023-02-01 22:20:21
pandas 实现将重复表格去重,并重新转换为表格的方法
2023-09-09 05:26:58
Python 实现进度条的六种方式
2022-09-26 02:22:08
python非标准时间的转换
2022-04-12 17:37:52
XML与HTML的结合(上)
2008-09-05 17:19:00
pytest解读一次请求多个fixtures及多次请求
2023-07-20 01:13:43
python数学建模之三大模型与十大常用算法详情
2023-10-04 17:59:19
python使用Windows的wmic命令监控文件运行状况,如有异常发送邮件报警
2022-01-13 09:48:25
python解析html提取数据,并生成word文档实例解析
2023-10-19 13:50:38
详解Django中Request对象的相关用法
2021-12-08 16:30:06
uniapp实现微信小程序的电子签名效果(附demo)
2024-04-19 09:55:39
Pandas中八个常用option设置的示例详解
2022-10-15 15:46:30
Access秘密技巧和陷阱
2008-04-12 14:55:00