哪些Android工具和方法最适合查找内存/资源泄漏?

哪些Android工具和方法最适合查找内存/资源泄漏?

我已经开发了一个Android应用程序,我的手机应用程序开发的时候,一切看起来都很好,你想宣布胜利和出海,但你知道,那里必须有一些内存和资源泄漏;在Android上只有1600万b堆,而且它显然很容易在Android应用程序中泄漏。

我一直在寻找,到目前为止,只能挖掘‘hprof’和‘traceview’的信息,没有得到很多好评。

您遇到或开发了哪些工具或方法,并希望在OS项目中共享这些工具或方法?


慕标琳琳
浏览 468回答 3
3回答

qq_花开花谢_0

如果您只是使用位图背景,那么@hp.android的答案很好,但是,在我的例子中,我有一个BaseAdapter提供一套ImageViewS代表aGridView..我修改了unbindDrawables()方法,以便条件是:if&nbsp;(view&nbsp;instanceof&nbsp;ViewGroup&nbsp;&&&nbsp;!(view&nbsp;instanceof&nbsp;AdapterView))&nbsp;{ &nbsp;&nbsp;...}但是问题是,递归方法从不处理AdapterView..为了解决这一问题,我做了以下工作:if&nbsp;(view&nbsp;instanceof&nbsp;ViewGroup)&nbsp;{ &nbsp;&nbsp;ViewGroup&nbsp;viewGroup&nbsp;=&nbsp;(ViewGroup)&nbsp;view; &nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;viewGroup.getChildCount();&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;unbindDrawables(viewGroup.getChildAt(i)); &nbsp;&nbsp;if&nbsp;(!(view&nbsp;instanceof&nbsp;AdapterView)) &nbsp;&nbsp;&nbsp;&nbsp;viewGroup.removeAllViews();}使孩子们AdapterView仍然被处理-该方法只是不尝试删除所有的子级(这是不支持的)。然而,这并不能完全解决这个问题,因为ImageViewS管理一个不是他们背景的位图。因此,我补充如下。这并不理想,但有效:if&nbsp;(view&nbsp;instanceof&nbsp;ImageView)&nbsp;{ &nbsp;&nbsp;ImageView&nbsp;imageView&nbsp;=&nbsp;(ImageView)&nbsp;view; &nbsp;&nbsp;imageView.setImageBitmap(null);}总体上unbindDrawables()方法是:private&nbsp;void&nbsp;unbindDrawables(View&nbsp;view)&nbsp;{ &nbsp;&nbsp;if&nbsp;(view.getBackground()&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;view.getBackground().setCallback(null); &nbsp;&nbsp;if&nbsp;(view&nbsp;instanceof&nbsp;ImageView)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;ImageView&nbsp;imageView&nbsp;=&nbsp;(ImageView)&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;imageView.setImageBitmap(null); &nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(view&nbsp;instanceof&nbsp;ViewGroup)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;ViewGroup&nbsp;viewGroup&nbsp;=&nbsp;(ViewGroup)&nbsp;view; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;viewGroup.getChildCount();&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;unbindDrawables(viewGroup.getChildAt(i)); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!(view&nbsp;instanceof&nbsp;AdapterView)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;viewGroup.removeAllViews(); &nbsp;&nbsp;}}我希望有一个更有原则的方法来释放这些资源。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android