Asaf Nevo
Asaf Nevo

Reputation: 11688

android Application class onLowMemory

i have an application which saves some objects in my Application class for global use. i don't want the Application class to be killed (because i need the information which is saved in it), so i've override the onLowMemory() function to make a Thread.sleep(1000)

what will happen when the Application class will awake if the memory is still low ? will call the onLowMemory() again ?

what is the right architecture to make sure this Application class won't be killed by system as long as my application and it's background services are running ?

Upvotes: 0

Views: 2593

Answers (2)

Aleksandar Stojadinovic
Aleksandar Stojadinovic

Reputation: 5049

You should not trust that function on being called. Android can kill your application whenever it wants, as long as it is in the background, and you can't really stop him or even attempt. It may call that function so your application can clear some caches or do anything it can do to free up some memory. That can be concluded from the documentation.

http://developer.android.com/reference/android/app/Application.html#onLowMemory()

Try to avoid relying on it. Do whatever you can to prepare your application for switching on OnPause, OnStop or onDestroy or similar methods that are sure to be called. That means saving variables, data or anything else that is important to a file or a database.

Upvotes: 0

njzk2
njzk2

Reputation: 39406

Don't do that !

  1. don't block system calls
  2. that doesn't prevent the system from killing your app
  3. the memory will still be low

There is no way to make sure your app doesn't get killed. save your data in a sharedpreference, a file, or a sqlite db, whichever suits your needs, but don't rely on the application instance.

Upvotes: 2

Related Questions