Monitor Retention Related Events Amongst Ontrimmemory(), Together With Copy Retention Depression Inward Android Emulator Using Adb

Posted on

Android tin sack reclaim retention from your app or kill your app solely if necessary to gratuitous upward retention for critical tasks. To care remainder the organisation retention too avoid the system’s demand to kill your app process, you lot tin sack implement the ComponentCallbacks2 interface too override onTrimMemory() callback method to psyche for retention related events when your app is inwards either the foreground or the background, too so liberate objects inwards reply to app lifecycle or organisation events that betoken the organisation needs to reclaim memory. 

When the organisation begins killing processes inwards the LRU cache, it primarily plant bottom-up. The organisation besides considers which processes swallow to a greater extent than retention too hence render the organisation to a greater extent than retention make if killed. The less retention you lot swallow spell inwards the LRU listing overall, the ameliorate your chances are to rest inwards the listing too last able to chop-chop resume.

Here is a instance code to implement the ComponentCallbacks2 interface too override onTrimMemory() callback method.

 packet com.blogspot.android_er.androidontrimmemory;  import android.content.ComponentCallbacks2; import android.os.Bundle; import; import android.util.Log;  /* Example to role onTrimMemory()  */ populace shape MainActivity extends AppCompatActivity         implements ComponentCallbacks2 {      person static terminal String TAG = "MyActivity";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          Log.v(TAG, "onCreate() called");     }      populace void onTrimMemory(int level){         Log.v(TAG, "onTrimMemory(" + degree + ")");         switch(level){              instance ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:                 Log.v(TAG, "TRIM_MEMORY_UI_HIDDEN");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:                 Log.v(TAG, "TRIM_MEMORY_RUNNING_MODERATE");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:                 Log.v(TAG, "TRIM_MEMORY_RUNNING_LOW");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:                 Log.v(TAG, "TRIM_MEMORY_RUNNING_CRITICA");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:                 Log.v(TAG, "TRIM_MEMORY_BACKGROUND");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_MODERATE:                 Log.v(TAG, "TRIM_MEMORY_MODERATE");                 break;             instance ComponentCallbacks2.TRIM_MEMORY_COMPLETE:                 Log.v(TAG, "TRIM_MEMORY_COMPLETE");                 break;              default:                 Log.v(TAG, "default");         }     }  }  

To copy the retention depression condition, you lot tin sack role the adb command:

adb compaction am send-trim-memory

Example for my case, it’s:

adb compaction am send-trim-memory com.blogspot.android_er.androidontrimmemory MODERATE

Notice that you lot cannot prepare background degree if your app is inwards foreground. Otherwise the next exception volition last thrown, equally shown inwards the video.

java.lang.IllegalArgumentException: Unable to prepare a background cut back degree on a foreground process

Overview of retention management
Manage your app’s memory