缓存设置多少合适呢,一般情况下,设置为当前可用内存的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;
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。