heihei blog

Blog icon by Unsplash https://unsplash.com/@virussinside

ActivityManager.isLowRamDevice

f:id:shoheikawano:20210131231239p:plain

理由は覚えていないがCoilのコード見ていてたまたま見つけた。

Coil Extensions.ktにあったのが以下のコード

internal inline val ActivityManager.isLowRamDeviceCompat: Boolean
    get() = SDK_INT < 19 || isLowRamDevice

https://github.com/coil-kt/coil/blob/master/coil-base/src/main/java/coil/util/Extensions.kt#L45-L46

端末のRAMが低いかどうかの真偽値を返す関数。Coilの場合はSDKバージョンが19未満 or ActivityManagerのこの関数がtrueを返すか、という判定の仕方をしていた。

ActivityManagerのコードは以下

/**
     * Returns true if this is a low-RAM device.  Exactly whether a device is low-RAM
     * is ultimately up to the device configuration, but currently it generally means
     * something with 1GB or less of RAM.  This is mostly intended to be used by apps
     * to determine whether they should turn off certain features that require more RAM.
     */
    public boolean isLowRamDevice() {
        return isLowRamDeviceStatic();
    }

https://developer.android.com/reference/android/app/ActivityManager#isLowRamDevice()

Javadocにあるとおり、メモリ消費の激しい特定の機能をOFFにすべきか?というときに利用できるよとのこと。基本的には1GB以下のRAMの端末とかの場合にtrueが返ってくる模様。 たとえばメモリ消費をめちゃするような機能?があるときに、「この端末では正しく動作しないことがあります」という文言を出す or 機能の導線を非表示にする、みたいな感じで実践で活用できるのかなー?もしくは動画系の3rd partyライブラリを作る際とかに活躍の場がありそう。