Reputation: 415
In android,is there any callback that is called when system runs low on memory or out of memory occurs.
For eg: there are three applications A,B,C running.In C out of memory is thrown.How can the application A come to know that so that it can clear its resources ,so that the android system wont kill the application A.
Upvotes: 2
Views: 2488
Reputation: 1486
override onLow memory method and do the stuff that you needed.
@Override
public void onLowMemory() {
// clear the unwanted resources from memory
super.onLowMemory();
}
NOTE: There is no guarantee that this method will be invoked correctly.
This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. While 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.
From developer Docs Link
Upvotes: 0
Reputation: 1715
Besides onLowMemory() there are other methods.
Use WeakReference to keep your cache and Android will handle OOM gracefully. Also, you can use a Service, which has higher priority than activities and will be destroyed after them.
Upvotes: 0
Reputation: 6044
You must extend the Application class, and override onLowMemory() method.
public class App extends Application {
@Override
public void onLowMemory() {
super.onLowMemory();
// handle lowmemory stuff
}
}
Upvotes: 2