緩存設置多少合適呢,一般情況下,設置爲當前可用內存的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; } }