惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - dail

IE浏览器在虚拟机中无法正常显示字符 jQuery在updatepanel中使用造成内存泄露 jQuery 1.7的隐藏改动 在javascript中实现类似asp.net webcontrol中的render的方法 jQuery 1.6的变化 - dail jQuery ui effects - dail jQuery ui 1.8.6 position 的一个bug - dail 一个progressbar widget jQuery编写widget的一些窍门 jquery animate动画的特殊用法。 一个简单的widget Jquery ui widget中的_create(),_init(),destroy() Jquery ui widget开发 Jquery ui css framework jquery animate Json.net简单用法 EXTJS学习(二)Message EXTJS学习(一) - dail jquery+linq制作博客(二)
在使用jQuery的时候不小心的内存泄漏
dail · 2012-04-08 · via 博客园 - dail

在写chart widget的时候,我们使用了第三方的svg类库Raphael。结果客户给我们报了一个内存泄漏的bug,我们在测试的时候确实存在内存泄漏。经过测试发现了问题的原因。Raphael本身存在一些问题,还有就是在和jQuery使用的时候,我们也没有注意到一个问题。

在创建jQuery对象的时候,jQuery会将jQuery缓存起来,放到jQuery.cache对象中。在调用jQuery的remove方法的时候,会清理掉jQuery的缓存。我们的问题就出在这里,在Raphael中,自己有remove方法来清楚Raphael创建的raphael对象和svg/vml元素。而在chart中,我们会使用jQuery在raphael创建的svg/vml元素上。这样在调用raphael的remove方法的时候,就不能移除jQuery缓存。由此造成了对象一直保留内存中。

在Raphael中,自己的animate和remove方法也存在说明,就是在animate的时候,会创建animate需要的一些对象缓存到raphael对象中,在调用stop方法的时候会清理掉这些元素。而当remove的时候,仅仅做了remove的事情,并没有移除掉这些对象。而在文档中也没有提到需要注意的问题。我们在使用的时候,就没有注意到正在做动画的元素在remove的时候直接调用remove方法,没有在之前调stop。这样也造成了对象不能销毁。

上述两个问题是造成内存泄漏的主要原因。在试用jQuery的时候,如果要删除dom对象的话,最好采用jQuery的remove方法,否则可能会出现诸如我们那样的问题。在jQuery的remove方法中还会清楚掉时间绑定等信息。