在一个 Android Honeycomb Activity 启动另一 Activity 后,如何释放 Drawable 内存?
我有一个 Android Honeycomb 应用程序,有两个活动;第一个有一个大的Bitmap
(从资源中获取)作为主LinearLayout
的背景,我发现将此元素的背景设置为null< Activity 的
onStop()
方法中的 /code> 可以节省大量内存,实际上大约 5MB。
然而,这似乎只有当我单击设备上的“睡眠”按钮时才起作用。如果我这样做,分析器就会显示 5MB 的大小会随您的喜好而消失。如果我启动第二个活动,则第一个活动的 onStop()
直到第二个活动的 onCreate()
之后才会被命中,并且探查器建议 BitmapDrawable
毕竟没有从内存中删除——所以它就坐在那里,五个看不见的无用兆字节,把这个地方弄得乱七八糟。
我可以从 onPause()
中删除它,这可能会更好,但我还使用各种 Dialog
元素的 onStop()
方法也将它们的背景绘图清空。他们似乎没有 onPause()
方法。
当一个活动启动另一个活动时,是否有推荐的方法来摆脱这些大型的、消耗内存的元素?
I have an Android Honeycomb application with two activities; the first one has a large Bitmap
(taken from resources) as a background to the main LinearLayout
and I have discovered that setting the background of this element to null
from the activity's onStop()
method can save a lot of memory—around 5MB, in fact.
However, this only seems to work when I click the "sleep" button on the device. If I do that the profiler shows the 5MB drops out of the world as easy as you like. If I start the second activity, the first activity's onStop()
doesn't get hit until after the second activity's onCreate()
and the profiler suggests that the BitmapDrawable
is not removed from memory after all—so there it sits, five invisible useless megabytes, cluttering the place up.
I could remove this from onPause()
, which might work better, but I'm also using the onStop()
methods of various Dialog
elements to null their background drawables as well. They don't seem to have onPause()
methods.
Is there a recommended method for getting rid of these kinds of large, memory-consuming elements as an activity launches another one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,OnPause 事件也是放置“无内存”代码的更好地方。
这里是从父活动打开子活动时的事件流
正如您已经注意到的,父活动的 onStop 仅在子活动创建完成后才会被调用。
从两个不同的位置调用无内存代码怎么样?考虑到对话框通常不会完全覆盖调用活动,因此保留其背景是有意义的。
也许当您打开新活动而不是对话框时可以设置一个标志,并且 OnPause 逻辑仅在启动新活动而不是对话框时才会删除背景。
Also for me OnPause event is a better place where put your "memory-free" code.
Here the event flow when you open a child activity from a parent activity
As you already noticed, parent's onStop is called only after complete child creation.
What about calling memory-free code from two different location? Consider that dialogs, generally, don't completely cover calling activity, so preserving it's background has a sense.
Maybe a flag can be set when you open a new activity instead of a dialog, and OnPause logic will drop background only when a new activity is launched, and not a dialog.
没有特殊的方法来处理这些事情,它是常规的java方式:
只要不要保留任何你不需要的东西的引用,就可以了。
查看此视频以获取有关此内容的详细信息:
http://www.youtube.com/watch?v= _CruQY55HOk
There is no special way for handling such things, it's regular java way:
Just don't keep a reference to anything you don't need, and you will be fine.
Check this video for great info on this:
http://www.youtube.com/watch?v=_CruQY55HOk