<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FoxLing - 前端开发 &#187; 内存泄露</title>
	<atom:link href="http://foxling.org/tag/%e5%86%85%e5%ad%98%e6%b3%84%e9%9c%b2/feed/" rel="self" type="application/rss+xml" />
	<link>http://foxling.org</link>
	<description>我还在寻找方向</description>
	<lastBuildDate>Tue, 10 Apr 2012 16:48:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Flex 中可能导致内存泄露的地方</title>
		<link>http://foxling.org/as-flex-air/flex-memory-leak/</link>
		<comments>http://foxling.org/as-flex-air/flex-memory-leak/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 17:49:11 +0000</pubDate>
		<dc:creator>FoxLing</dc:creator>
				<category><![CDATA[AS/Flex/AIR]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Memory Leak]]></category>
		<category><![CDATA[内存泄露]]></category>

		<guid isPermaLink="false">http://foxling.cn/?p=392</guid>
		<description><![CDATA[收集了不少的会导致内存泄露的情况： 事件监听： 对父级对象加了监听函数，会造成内存泄露，例： override protected function mouseDownHandler(…):void { systemManager.addEventListener(“mouseUp”, mouseUpHandler); ...... } 解决： 在销毁对象的时候，remove掉这些监听，虽然弱引用也可以避免这些问题，但自己掌控感觉更好。 但以下几种情况不会造成内存泄露： 弱引用：someObject.addEventListener(MouseClick.CLICK, handlerFunction, false, 0, true); 对自己的引用：this.addEventListener(MouseClick.CLICK, handlerFunction); 子对象的引用： private var childObject:UIComponent = new UIComponent; addChild(childObject); childObject.addEventListener(MouseEvent.CLICK, clickHandler); 总之&#8230;有addEventListener，就removeEventListener一下吧，要为自己做的事负责~哈哈 清除引用 remove掉子对象后并不会删除该对象，他还保留在内存中，应该将引用设置为null removeChildren(obj); obj = null; 静态成员 Class （或MXML）中有： public static var _eventService : MyService=new MyService(); 解决：在dispose时，需要设置： _eventService =null; module (未解决) moduleLoader [...]]]></description>
			<content:encoded><![CDATA[<p>收集了不少的会导致内存泄露的情况：</p>
<h3>事件监听：</h3>
<p>对父级对象加了监听函数，会造成内存泄露，例：</p>
<pre>
override protected function mouseDownHandler(…):void {
systemManager.addEventListener(“mouseUp”, mouseUpHandler);
......
}
</pre>
<p>解决：<br />
在销毁对象的时候，remove掉这些监听，虽然弱引用也可以避免这些问题，但自己掌控感觉更好。</p>
<p>但以下几种情况不会造成内存泄露：</p>
<ul>
<li>弱引用：<code>someObject.addEventListener(MouseClick.CLICK, handlerFunction, false, 0, true);</code></li>
<li>对自己的引用：<code>this.addEventListener(MouseClick.CLICK, handlerFunction);</code></li>
<li>子对象的引用：
<pre>
private var childObject:UIComponent = new UIComponent;
addChild(childObject);
childObject.addEventListener(MouseEvent.CLICK, clickHandler);
</pre>
</li>
</ul>
<p>总之&#8230;有addEventListener，就removeEventListener一下吧，要为自己做的事负责~哈哈</p>
<h3>清除引用</h3>
<p>remove掉子对象后并不会删除该对象，他还保留在内存中，应该将引用设置为null</p>
<pre>
removeChildren(obj);
obj = null;
</pre>
<p><span id="more-392"></span></p>
<h3>静态成员</h3>
<p>Class （或MXML）中有：</p>
<pre>public static var _eventService : MyService=new MyService();</pre>
<p>解决：在dispose时，需要设置：</p>
<pre>_eventService =null;</pre>
<h3>module (未解决)</h3>
<p>moduleLoader unloadModule后<br />
ModuleInfo 并不会被GC.<br />
<a href="http://www.nutrixinteractive.com/blog/?p=132">Garbage Collection in a MultiCore Modular Pipes Application</a><br />
这篇文章介绍了一种GC策略，感觉对于ModuleInfo 的GC无效。<br />
(未尝试、未遇到)</p>
<h3>CSS样式</h3>
<p>module 中如果使用了shell的CSS定义或是&lt;mx:Style&gt; 这样的定义，那么这个module将不能GC.<br />
弹出的窗口应该是同样的结果.<br />
解决方法，使用动态CSS文件<br />
module init中</p>
<pre>StyleManager.loadStyleDeclarations("css/myStyle.swf");</pre>
<p>module dispose中</p>
<pre>StyleManager.unloadStyleDeclarations("css/myStyle.swf");</pre>
<h3>TextInput/Textarea(未解决)</h3>
<p>如果module中有window使用了TextInput/Textarea控件，不点击没有问题，只要点上去，那么很遗憾了，module和所在窗体将不能被GC.<br />
这个BUG非常严重，目前还没有解决方法。<br />
<a href="http://bugs.adobe.com/jira/browse/SDK-14781">memory leak when using TextInput and TextArea when click the keyboard</a> 这里面附加的解决方法无效。<br />
通过profiler分析，应该和Focusmanager有关，只有一点击就不会释放。</p>
<h3>CursorManager.setCursor</h3>
<p>使用了</p>
<pre>cursorID = CursorManager.setCursor(iconClosed);</pre>
<p>dispose时要</p>
<pre>CursorManager.removeCursor(cursorID); </pre>
<h3>Bitmap</h3>
<p>如果使用Bitmap，结束时需要调用其dispose方法，否则内存消耗巨大。<br />
另外，BitmapData是可以共享使用的，多个Bitmap可以使用同一BitmapData，节省不少内存。</p>
<pre>
var bmp:Bitmap  =new Bitmap();

........

if (bmp.bitmapData!=null) {
    bmp.bitmapData.dispose();
}
</pre>
<h3>Image</h3>
<p>包含了Image对象时，在removeChildren时会造成不能释放（测试多次，结果不一，建议还是做如下处理）。<br />
解决：</p>
<pre>
img.source = null;
this.removeChild(img);
img = null;
</pre>
<h3>Loader、SWFLoader、声音、视频、Effect等&#8230;</h3>
<ul>
<li>如果是加载SWF文件，先停止播放。</li>
<li>停止声音的播放</li>
<li>停止正在播放的影片剪辑(Movieclip)</li>
<li>关闭任何连接的网络对象，例如Loader正在加载，要先close。</li>
<li>取消对摄像头或者麦克风的引用</li>
<li>取消事件监听器</li>
<li>停止任何正在运行的定时器，clearInterval()</li>
<li>停止任何Timer对象，timer.stop()</li>
<li>停止正在播放的效果(Effect)</li>
</ul>
<h3>其他</h3>
<p>binding也疑似有memory leak 问题。</p>
<p>引用以及内存泄露相关博文和资料：<br />
<a href="http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html">http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html</a><br />
<a href="http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/">http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/</a><br />
<a href="http://www.cnblogs.com/janyou/archive/2008/11/25/1340753.html">http://www.cnblogs.com/janyou/archive/2008/11/25/1340753.html</a><br />
<a href="http://www.dreamingwell.com/articles/archives/2008/05/understanding_m.php">http://www.dreamingwell.com/articles/archives/2008/05/understanding_m.php</a></p>
<p>总结：由于之前Flash一直是在网页上使用，一般网页都是看完就关掉的，估计Adobe在内存回收这块也没有下太大的功夫，现在AIR的出现使得内存管理也相当重要了，并且，AIR本身对内存的消耗就相当大，一个没有任何内容的初始创建的AIR程序，就得占掉10-20M+的内存&#8230;AIR还需改善.</p>
]]></content:encoded>
			<wfw:commentRss>http://foxling.org/as-flex-air/flex-memory-leak/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

