When the APP memory is insufficient, we need to do some memory release operations to avoid app running out of time, or delay the app survival time as much as possible to reduce the probability of being recycled by the system.

How to listen for ComponentCallbacks

So how do you listen for these moments? The Application, Activity, Service and ContentProvider of the system have implemented the Interface ComponentCallbacks2, we can easily obtain these opportunities. In addition to these time, we can also through Context# registerComponentCallbacks to add your own listener.

OnTrimMemory (@trimMemoryLevel int level) ComponentCallbacks2 (@trimMemoryLevel int level) It also inherits the ComponentCallbacks interface, so it can also listen for the system’s onConfigurationChanged(@nonNULL Configuration newConfig) and onLowMemory() callbacks.

Within these callbacks, we can do targeted operations to free memory and resources.

ComponentCallbacks2 source code:

public interface ComponentCallbacks2 extends ComponentCallbacks {

    /** @hide */
    @IntDef(prefix = { "TRIM_MEMORY_" }, value = {
            TRIM_MEMORY_COMPLETE,
            TRIM_MEMORY_MODERATE,
            TRIM_MEMORY_BACKGROUND,
            TRIM_MEMORY_UI_HIDDEN,
            TRIM_MEMORY_RUNNING_CRITICAL,
            TRIM_MEMORY_RUNNING_LOW,
            TRIM_MEMORY_RUNNING_MODERATE,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface TrimMemoryLevel {}

    static final int TRIM_MEMORY_COMPLETE = 80;

    static final int TRIM_MEMORY_MODERATE = 60;
    
    static final int TRIM_MEMORY_BACKGROUND = 40;

    static final int TRIM_MEMORY_UI_HIDDEN = 20;

    static final int TRIM_MEMORY_RUNNING_CRITICAL = 15;

    static final int TRIM_MEMORY_RUNNING_LOW = 10;

    static final int TRIM_MEMORY_RUNNING_MODERATE = 5;

    void onTrimMemory(@TrimMemoryLevel int level);
}
Copy the code

ComponentCallbacks source code:

public interface ComponentCallbacks {

    void onConfigurationChanged(@NonNull Configuration newConfig);

    void onLowMemory();
}
Copy the code

How do you mock these times?

Killing processes on the web is generic, and you can’t mock all The Times, so it’s not accepted here.

After much searching, I found a command that can mockonTrimMemory(@trimmemoryLevel int level) for all levels in the mockonTrimMemory(@trimmemoryLevel int level) method in the following format:

adb shell am send-trim-memory <package-name> <level>
Copy the code

E.g. : level can be a constant string can also be a corresponding Numbers, with ComponentCallbacks2. The value is TrimMemoryLevel one-to-one correspondence

The adb shell am send - trim - memory com. Tinytongtong. Androidstudy MODERATE or:  adb shell am send-trim-memory com.tinytongtong.androidstudy 5Copy the code

The specific mapping relationship of level is shown in the table:

TrimMemoryLevel The string constant corresponding to level Level Indicates the number
TRIM_MEMORY_COMPLETE COMPLETE 80
TRIM_MEMORY_MODERATE MODERATE 60
TRIM_MEMORY_BACKGROUND BACKGROUND 40
TRIM_MEMORY_UI_HIDDEN HIDDEN 20
TRIM_MEMORY_RUNNING_CRITICAL RUNNING_CRITICAL 15
TRIM_MEMORY_RUNNING_LOW RUNNING_LOW 10
TRIM_MEMORY_RUNNING_MODERATE RUNNING_MODERATE 5

Examples of all commands:

adb shell am send-trim-memory com.tinytongtong.androidstudy COMPLETE

adb shell am send-trim-memory com.tinytongtong.androidstudy MODERATE

adb shell am send-trim-memory com.tinytongtong.androidstudy BACKGROUND

adb shell am send-trim-memory com.tinytongtong.androidstudy HIDDEN

adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_CRITICAL

adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_LOW

adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_MODERATE
Copy the code

reference

Stackoverflow.com/questions/3…