开发者

How does onLowMemory Work on android?

I would like to know how onLowMemory() function gets executed

for eg.

Say I have 3 activities each with their onLowMemory() functions overridden to clean up data from RAM. I also have 1 Application class which also overrides onLowMemory() to clean up some global state data.

Now lets say we go from activity A -> activity B -> activity C , and on activity C we run out of memory. My question is what happens after that?

From what I understand, onLowMemory() function of Activity C and Application class will get called, am I correct? Does onLowMemory() function of activity A and B gets called ?

Also I believe Activity A and B will be killed (since they are background activiti开发者_Python百科es), but before those activities are killed, do their respective onLowMemory() gets called?


When the phone's memory is low, the background processes will be killed by framework. If the last background process is killed, the framework will call onLowMemory of every app.

This is described in source codes. See: https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/am/ActivityManagerService.java#L3088

  // If there are no longer any background processes running,
  // and the app that died was not running instrumentation,
  // then tell everyone we are now low on memory.


According to the Documentation http://developer.android.com/reference/android/app/Application.html#onLowMemory%28%29

the exact point at which this will be called is not defined, generally it will happen around the time all background process have been killed, that is before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.


google talks about this function in one of their videos:

http://www.youtube.com/watch?v=gbQb1PVjfqM&feature=player_embedded#t=682s

however, i think it's still unclear when such a method is being called , and if it can help avoid our app being killed as a result of having a lot of processes and that ours take a lot of memory .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜