OnTrimMemory使用与举例

OnTrimMemory使用与举例

OnTrimMemory 回调是 Android 4.0 之后提供的一个API,这个 API 是提供给开发者的,它的主要作用是提示开发者在系统内存不足的时候,通过处理部分资源来释放内存,从而避免被 Android 系统杀死。

可实现OnTrimMemory的类

Application.onTrimMemory()
Activity.onTrimMemory()
Fragement.OnTrimMemory()
Service.onTrimMemory()
ContentProvider.OnTrimMemory()

API简介

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 官方地址:
     * https://developer.android.com/reference/android/content/ComponentCallbacks2.html
     *
     * @param level
     */
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        switch (level) {
            // 80   后台进程
            // 当前手机目前内存吃紧 (后台进程数量少),系统开始根据LRU缓存来清理进程,而该程序位于LRU缓存列表的最边缘位置,系统会先杀掉该进程,应尽释放一切可以释放的内存。
            // 官方文档:the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.
            case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
                break;
            // 60   后台进程
            //当前手机目前内存吃紧(后台进程数量少),系统开始根据LRU缓存来清理进程,而该程序位于LRU缓存列表的中间位置,应该多释放一些内存提高运行效率。
            // 官方文档:the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.
            case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
                break;
            // 40   后台进程
            // 当前手机目前内存吃紧(后台进程数量少),系统开始根据LRU缓存来清理进程,而该程序位于LRU缓存列表的头部位置,不太可能被清理掉的,但释放掉一些比较容易恢复的资源能够提高手机运行效率,同时也能保证恢复速度。
            // 官方文档:the process has gone on to the LRU list. This is a good opportunity to clean up resources that can efficiently and quickly be re-built if the user returns to the app
            case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
                break;
            // 20   后台进程
            // 当前应用程序的所有UI界面不可见,一般是用户点击了Home键或者Back键,导致应用的UI界面不可见,这时应该释放一些UI相关资源,TRIM_MEMORY_UI_HIDDEN是使用频率最高的裁剪等级。
            // 官方文档:the process had been showing a user interface, and is no longer doing so. Large allocations with the UI should be released at this point to allow memory to be better managed
            case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
                break;
            // 15   前台RUNNING进程
            // 表示该进程是前台或可见进程,但是目前手机比较内存十分吃紧(后台及空进程基本被全干掉了),这时应当尽可能地去释放任何不必要的资源,否则,系统可能会杀掉所有缓存中的进程,并且杀一些本来应当保持运行的进程。
            // 官方文档:the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running. Your running process should free up as many non-critical resources as it can to allow that memory to be used elsewhere. The next thing that will happen after this is called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user.
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
                break;
            // 10   前台RUNNING进程
            // 表示该进程是前台或可见进程,正常运行,一般不会被杀掉,但是目前手机比较吃紧(后台及空进程被全干掉了一大波),应该去释放掉一些不必要的资源以提升系统性能。
            // 官方文档:the process is not an expendable background process, but the device is running low on memory. Your running process should free up unneeded resources to allow that memory to be used elsewhere.
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
                break;
            // 5    前台RUNNING进程
            // TRIM_MEMORY_RUNNING_MODERATE 表示该进程是前台或可见进程,正常运行,一般不会被杀掉,但是目前手机有些吃紧(后台及空进程存量不多),系统已经开始清理内存,有必要的话,可以释放一些内存。
            // 官方文档:the process is not an expendable background process, but the device is running moderately low on memory. Your running process may want to release some unneeded resources for use elsewhere。
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
                break;
        }
    }
}

使用举例

Contact.java

@Override
public void onTrimMemory(int level) {
    if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
        // Clear the caches.  Note all pending requests will be removed too.
        clear();
    }
}

public void clear() {
    mPendingRequests.clear();
    mBitmapHolderCache.evictAll();
    mBitmapCache.evictAll();
}

参考:

Android进程保活-自“裁”或者耍流氓
从OnTrimMemory角度谈Android代码内存优化
Android进程保活全攻略(上)

Redis 是一个高性能的内存数据存储系统,常用于缓存、消息队列、会话管理等场景。以下是一些 Redis 的使用场景举例: 1. 缓存:Redis 可以用作缓存存储,将经常访问的数据存储在内存中,以提高读取速度。例如,可以将数据库查询结果缓存在 Redis 中,避免频繁查询数据库。 2. 消息队列:Redis 提供了发布/订阅机制,可以用作消息队列。生产者将消息发布到特定的频道,消费者订阅该频道并接收消息。这在需要解耦和异步处理的场景中非常有用。 3. 分布式锁:Redis 支持原子操作和高性能,因此可以用于实现分布式锁。通过 Redis 的 SETNX 命令可以实现基于 Redis 的分布式锁,确保在分布式环境下对共享资源进行互斥访问。 4. 计数器和排行榜:Redis 的原子操作特性使其适用于计数器和排行榜的实现。可以使用 INCRBY 命令来实现递增或递减的计数器,并使用有序集合来存储和排序排行榜数据。 5. 会话管理:Redis 可以用于存储会话数据,以提高网站的性能和可伸缩性。将用户的会话数据存储在 Redis 中,可以实现分布式会话管理,并且由于 Redis 的高性能,可以有效处理大量的并发请求。 这些只是 Redis 的一些常见使用场景,实际上 Redis 还有很多其他应用领域,如地理位置处理、分布式缓存、实时消息传递等。根据具体需求和场景,可以灵活运用 Redis 来解决各种问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bjxiaxueliang

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值