温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LruCache大小的定义

发布时间:2020-10-06 05:03:35 来源:网络 阅读:404 作者:IT学无止境 栏目:开发技术

缓存设置多少合适呢,一般情况下,设置为当前可用内存的8分之1,那么就需要先获取当前可用内存是多少,通过以下代码可以知道当前缓存的大小:

final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();

得到当前缓存的大小后,即可对缓存的大小进行设置,代码如下:

public class KaleApplication extends Application{    
    /**
     * @description 
     *
     * @param context
     * @return 得到需要分配的缓存大小,这里用八分之一的大小来做     */
    public int getMemoryCacheSize() {        // Get memory class of this device, exceeding this amount will throw an        // OutOfMemory exception.
        final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();        // Use 1/8th of the available memory for this memory cache.
        return 1024 * 1024 * memClass / 8;
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI