问题描述
我最近在 管理应用程序的内存中阅读了这篇文章 ,如果您是AndroidDev且从未使用过,我强烈建议您阅读.
有很多好的做法,我从未碰过的一件事是 onTrimMemory(int级别)方法,以通知应该或应该释放哪些内存的事件.
这是该文章的引文:
我真的很想在我的应用程序中实现良好的内存管理,因此我希望实现 onTrimMemory()正确的方式.
对此我只有几个问题:
-
是 onTrimMemory(TRIM_MEMORY_UI_HIDDEN)在onStop()之后调用?
-
在这种情况下,释放UI资源"是什么意思?仅用于清理位图缓存,还是实际上删除并销毁视图树中的每个视图?我通常会破坏 onDestroy()或 onDestroyView()方法中的视图,我现在想知道自己是否做对了.
-
是否有对 onTrimMemory(TRIM_MEMORY_UI_HIDDEN)?例如 onCreate-onDestroy , onStart-onStop , onCreateView-onDestroyView .我要了解在 onTrimMemory(TRIM_MEMORY_UI_HIDDEN)已被调用.
- 级别为TRIM_MEMORY_UI_HIDDEN的
-
onTrimMemory实际上是在onStop之前调用的.调用onStop时,这意味着该活动实际上正在停止,并且如果需要,Android OS可能会立即将其终止,因此,除了onRestart有时是onDestroy之外,您不应期望对该活动的回调进行更多的调用. /p>
-
释放您的UI资源"实际上是关于诸如缓存之类的东西.通常,您不必担心管理视图或UI组件,因为操作系统已经这样做了,这就是为什么要使用所有这些回调来创建,开始,暂停,停止和销毁活动的原因.但是,有时要提高性能,就必须增加内存使用率,例如缓存活动使用的某些数据.这是在调用onTrimMemory时应释放的资源类型,因此,即使它影响性能,您的应用程序也会使用更少的内存.但是,您应该担心内存泄漏.如果您的活动停止,请确保不要保留对其视图的任何引用,因为这将使该活动不会被垃圾收集,这将使整个上下文无法被收集,这很糟糕,主要是在您希望保持应用程序运行的情况下数小时或数天(如实施服务时).
-
否,没有对应的onTrimMemory回调.但是,您不需要一个.如前所述,如果保留一些资源的缓存以提高性能,则将其清空,并在需要时使其再次增长.如果内存水平保持较低,则可能会很快以相同的内存水平再次调用onTrimMemory.顺便说一句,请记住,将使用几种不同的内存级别而不是TRIM_MEMORY_UI_HIDDEN来调用onTrimMemory.
I recently read this article on Managing Your App's Memory,I strongly suggest to read it if you are an AndroidDev and never did.
There are lots of good practices and one thing I never happen to know about is the onTrimMemory(int level) method called by the system on every Activity/Fragment to notify events on which memory should or could be released.
Here is a quote from that article:
I am really interested in implementing a good memory management in my application so I am looking forward to implement the onTrimMemory() in the right way.
I only have a few questions about it:
is onTrimMemory(TRIM_MEMORY_UI_HIDDEN) called right after the onStop()?
what "release your UI resources" means in that context? just for instance clean the Bitmap cache, or actually remove and destroy every View in the View tree? i usually destroy the Views in the onDestroy() or onDestroyView() methods, i am now wondering if i'm doing it right.
is there a Twin/correspondent callback to onTrimMemory(TRIM_MEMORY_UI_HIDDEN)? like onCreate-onDestroy, onStart-onStop, onCreateView-onDestroyView. I'm asking to understand where and how i should restore the UI state after an Activity/Fragment as been brought in foreground after onTrimMemory(TRIM_MEMORY_UI_HIDDEN) has been called.
onTrimMemory with TRIM_MEMORY_UI_HIDDEN level is actually called before onStop. When onStop is called, it means the activity is really stopping, and the Android OS might kill it right away if it needs to, so you should not expect any more calls to that activity's callbacks aftet that, except for onRestart and sometimes onDestroy.
"release your UI resources" is actually about things like caches. You usually don't have to worry about managing views or UI components because the OS already does that, and that's why there are all those callbacks for creating, starting, pausing, stopping and destroying an activity. However, sometimes to improve performance you have to increase memory usage, such as caching some data used by your activities. That's the type of resource you should release when onTrimMemory is called, so your app uses less memory, even if it affects performance. You should worry about memory leaks though. If your activity stops, be sure not to keep any references to its views, because that would keep the activity from being garbage collected, which would keep the whole context from being collected, and that's bad, mostly if you want to keep your app running for hours or days (like when you implement services).
No, there's no correspondent callback to onTrimMemory. However, you shouldn't need one. As I said before, if you keep a cache of some resources to improve performance, just empty that, and let it grow again if it needs to. If memory level keeps low, onTrimMemory may be called again soon, with that same memory level. By the way, keep in mind that onTrimMemory will be called with several different memory levels, not just TRIM_MEMORY_UI_HIDDEN.
这篇关于了解onTrimMemory(int level)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!