public static synchronized void initialize(Configuration configuration) { if (sIsInitialized) { Log.v("ActiveAndroid already initialized."); return; } sContext = configuration.getContext(); sModelInfo = new ModelInfo(configuration); sDatabaseHelper = new DatabaseHelper(configuration); // TODO: It would be nice to override sizeOf here and calculate the memory // actually used, however at this point it seems like the reflection // required would be too costly to be of any benefit. We'll just set a max // object size instead. sEntities = new LruCache<String, Model>(configuration.getCacheSize()); openDatabase(); sIsInitialized = true; Log.v("ActiveAndroid initialized successfully."); }
private void initMemCache() { // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getByteCount() / 1024; } }; }
public EmojiCache(int cacheSize) { mCache = new LruCache<Integer, Drawable>(cacheSize) { @Override protected int sizeOf(Integer key, Drawable value) { return 1; }; @Override protected void entryRemoved(boolean evicted, Integer key, Drawable oldValue, Drawable newValue) { //这种情况,可能该drawable还在页面使用中,不能随便recycle。这里解除引用即可,gc会自动清除 // if (oldValue instanceof BitmapDrawable) { // ((BitmapDrawable)oldValue).getBitmap().recycle(); // } } }; }
private static LruCache<String, Bitmap> getBitmapCache() { ThreadUtils.assertOnUiThread(); LruCache<String, Bitmap> cache = sBitmapCache == null ? null : sBitmapCache.get(); if (cache != null) return cache; // Create a new weakly-referenced cache. cache = new LruCache<String, Bitmap>(MAX_CACHE_BYTES) { @Override protected int sizeOf(String key, Bitmap thumbnail) { return thumbnail == null ? 0 : thumbnail.getByteCount(); } }; sBitmapCache = new WeakReference<>(cache); return cache; }
private void setupCaches() { // Titles and subtitles are small enough to stay cached, so use a very high max size bodyTitleCache = new LruCache<>(10000); bodySubtitleCache = new LruCache<>(10000); // Every artwork item will be a BitmapDrawable, so use the bitmap byte count for sizing bodyArtworkCache = new LruCache<LibraryItem, Drawable>(1000000) { @Override protected int sizeOf(final LibraryItem key, final Drawable value) { return ((BitmapDrawable) value).getBitmap().getByteCount(); } }; // Header cache will only contain one item headerTitleCache = new LruCache<>(2); headerSubtitleCache = new LruCache<>(2); headerArtworkCache = new LruCache<>(2); }
/** * Initialises the testing objects and assigns them to member variables. */ @Before public void setup() throws LibraryReadException { artwork = mock(Drawable.class); when(artwork.getIntrinsicWidth()).thenReturn(100); when(artwork.getIntrinsicHeight()).thenReturn(100); defaultArtwork = mock(Drawable.class); when(defaultArtwork.getIntrinsicWidth()).thenReturn(100); when(defaultArtwork.getIntrinsicHeight()).thenReturn(100); cachedArtwork = mock(Drawable.class); when(cachedArtwork.getIntrinsicWidth()).thenReturn(100); when(cachedArtwork.getIntrinsicHeight()).thenReturn(100); libraryItem = mock(LibraryItem.class); when(libraryItem.getArtwork(anyInt(), anyInt())).thenReturn(artwork); cache = new LruCache<>(10); displayableDefaults = mock(DisplayableDefaults.class); when(displayableDefaults.getArtwork()).thenReturn(defaultArtwork); imageView = mock(ImageView.class); }
/** * Initialises the testing objects and assigns them to member variables. */ @Before public void setup() throws LibraryReadException { subtitle = mock(CharSequence.class); defaultSubtitle = mock(CharSequence.class); cachedSubtitle = mock(CharSequence.class); libraryItem = mock(LibraryItem.class); when(libraryItem.getSubtitle()).thenReturn(subtitle); cache = new LruCache<>(10); displayableDefaults = mock(DisplayableDefaults.class); when(displayableDefaults.getSubtitle()).thenReturn(defaultSubtitle); textView = mock(TextView.class); }
/** * Initialises the testing objects and assigns them to member variables. */ @Before public void setup() throws LibraryReadException { title = mock(CharSequence.class); defaultTitle = mock(CharSequence.class); cachedTitle = mock(CharSequence.class); libraryItem = mock(LibraryItem.class); when(libraryItem.getTitle()).thenReturn(title); cache = new LruCache<>(10); displayableDefaults = mock(DisplayableDefaults.class); when(displayableDefaults.getTitle()).thenReturn(defaultTitle); textView = mock(TextView.class); }
/** * 初始化<硬盘缓存>和<内存缓存> * @param context context * @Description: */ private void init(Context context) { //设置硬盘缓存 mImageDiskCache = ImageDiskLruCache.openImageCache(context, CacheConfig.Image.DISK_CACHE_NAME, CacheConfig.Image.DISK_CACHE_MAX_SIZE); // 设置内存缓存. final int imageMemorySize = CacheUtils.getMemorySize(context, CacheConfig.Image.MEMORY_SHRINK_FACTOR); LogUtil.d(TAG, "memory size : " + imageMemorySize); mMemoryCache = new LruCache<String, Bitmap>(imageMemorySize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return CacheUtils.getBitmapSize(bitmap); } @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { } }; }
public EmojiCache(int cacheSize) { mCache = new LruCache<Integer, Drawable>(cacheSize) { @Override protected int sizeOf(Integer key, Drawable value) { return 1; } @Override protected void entryRemoved(boolean evicted, Integer key, Drawable oldValue, Drawable newValue) { //这种情况,可能该drawable还在页面使用中,不能随便recycle。这里解除引用即可,gc会自动清除 // if (oldValue instanceof BitmapDrawable) { // ((BitmapDrawable)oldValue).getBitmap().recycle(); // } } }; }
/** * Constructor * @author leibing * @createTime 2017/3/2 * @lastModify 2017/3/2 * @param context ref * @return */ private JkImageLoader(Context context){ // init application conext mApplicationContext = context.getApplicationContext(); // init memory cache(one 8 of the max memory) int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } }; // init disk cache initDiskCache(); }
/** * Initialize the cache. * * @param memCacheSizePercent The cache size as a percent of available app memory. */ private void init(float memCacheSizePercent) { int memCacheSize = calculateMemCacheSize(memCacheSizePercent); // Set up memory cache if (BuildConfig.DEBUG) { Log.d(TAG, "Memory cache created (size = " + memCacheSize + ")"); } mMemoryCache = new LruCache<String, Bitmap>(memCacheSize) { /** * Measure item size in kilobytes rather than units which is more practical * for a bitmap cache */ @Override protected int sizeOf(String key, Bitmap bitmap) { final int bitmapSize = getBitmapSize(bitmap) / 1024; return bitmapSize == 0 ? 1 : bitmapSize; } }; }
/** * create thumbnail cache */ @SuppressLint("NewApi") private final void createBitmapCache(final boolean clear) { if (clear && (sThumbnailCache != null)) { clearBitmapCache(hashCode()); } if (sThumbnailCache == null) { // use 1/CACHE_RATE of available memory as memory cache final int cacheSize = 1024 * 1024 * mMemClass / CACHE_RATE; // [MB] => [bytes] sThumbnailCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // control memory usage instead of bitmap counts return bitmap.getRowBytes() * bitmap.getHeight(); // [bytes] } }; } ThreadPool.preStartAllCoreThreads(); }
public static void settingCache() { // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getByteCount() / 1024; } }; }
private MemoryCacheManager() { // 取本虚拟机可用的最大内存的1/16作为内存缓存区 int maxMemorySize = (int)Runtime.getRuntime().maxMemory(); int maxCacheSize = maxMemorySize >> 4; mMemoryLruCache = new LruCache<String, T>(maxCacheSize) { @Override protected int sizeOf(String key, T value) { return value.getSize(); } @Override protected void entryRemoved(boolean evicted, String key, T oldValue, T newValue) { if (oldValue != null) oldValue.release(); } }; }
public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) { mContext = context; mContentResolver = context.getContentResolver(); mInflater = LayoutInflater.from(context); mPreferredMaxResultCount = preferredMaxResultCount; if (mPhotoCacheMap == null) { mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE); } mQueryType = queryMode; if (queryMode == QUERY_TYPE_EMAIL) { mQuery = Queries.EMAIL; } else if (queryMode == QUERY_TYPE_PHONE) { mQuery = Queries.PHONE; } else { mQuery = Queries.EMAIL; Log.e(TAG, "Unsupported query type: " + queryMode); } }
@Inject BitmapCache() { // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; cache = new LruCache<Uri, Bitmap>(cacheSize) { @Override protected int sizeOf(Uri key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getByteCount() / 1024; } }; }
/** * Basic constructor, builds the memory cache automatically. */ public EpicBitmapCache() { // Get max available VM memory, exceeding this amount will throw an // OutOfMemory exception. Stored in kilobytes as LruCache takes an // int in its constructor. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // Use 1/8th of the available memory for this memory cache. final int cacheSize = maxMemory / 8; //Initialize the memory cache mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024; } }; }
public MemCache(CachePolicy cachePolicy) { int poolSize = cachePolicy.getMemCachePoolSize(); mLruCache = new LruCache<String, Bitmap>(poolSize) { @Override protected int sizeOf(String key, Bitmap value) { if (value == null) return 0; return value.getWidth() * value.getHeight(); } @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { super.entryRemoved(evicted, key, oldValue, newValue); // Hard to recycle. oldValue = null; } }; }
/** * 初始化 * * @param threadCount * @param type */ private void init(int threadCount, Type type,Context context) { // 获取我们应用的最大可用内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheMemory = maxMemory / 8; //注意此处要获取全局Context,避免引用Activity造成资源无法释放 mContext=context.getApplicationContext(); mLruCache = new LruCache<String, Bitmap>(cacheMemory){ @Override protected int sizeOf(String key, Bitmap value) { // return value.getAllocationByteCount(); return value.getRowBytes() * value.getHeight(); //旧版本方法 } }; // 创建线程池 mThreadPool = Executors.newFixedThreadPool(threadCount); mType = type; mSemaphoreThreadPool = new Semaphore(threadCount,true); mTaskQueue = new LinkedBlockingDeque<Runnable>(); initBackThread(); }
private TransformationAdapterWrapper(@NonNull Builder builder) { this.inflater = LayoutInflater.from(builder.context); this.innerAdapter = builder.innerAdapter; this.rows = builder.rows; this.columns = builder.columns; this.marginTop = builder.marginTop; this.imagesCache = new LruCache<>(OFFSET_PAGES); this.translationX = builder.translationX; this.translationY = builder.translationY; this.piecesSpacing = builder.piecesSpacing; this.complexViewDetector = builder.complexViewDetector; this.bitmapTransformerFactory = builder.bitmapTransformerFactory; this.bitmapScale = builder.bitmapScale; this.itemsMap = new HashMap<>(); this.positionsMap = new HashMap<>(); }
/** * 初始化 * * @param threadCount * @param type */ private void init(int threadCount, Type type) { initBackThread(); // 获取我们应用的最大可用内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheMemory = maxMemory / 8; mLruCache = new LruCache<String, Bitmap>(cacheMemory) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; // 创建线程池 mThreadPool = Executors.newFixedThreadPool(threadCount); mTaskQueue = new LinkedList<Runnable>(); mType = type; mSemaphoreThreadPool = new Semaphore(threadCount); }
public CurrentPlaylistAdapter(Context context, PlaybackServiceConnection playbackServiceConnection) { super(); mContext = context; try { mPBS = playbackServiceConnection.getPBS(); mPlaylistSize = mPBS.getPlaylistSize(); mCurrentPlayingIndex = mPBS.getCurrentIndex(); } catch (RemoteException e) { mPBS = null; e.printStackTrace(); } mArtworkManager = ArtworkManager.getInstance(context.getApplicationContext()); mTrackCache = new LruCache<>(CACHE_SIZE); }
public SDCardImageLoader(int screenW, int screenH) { this.screenW = screenW; this.screenH = screenH; // 获取应用程序最大可用内存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 8; // 设置图片缓存大小为程序最大可用内存的1/8 imageCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; }
private MapList2(List<T> list, List<T2> list2, Mappable2<T, T2, E> mapper, ListMappable2<T, T2, E> indexMapper) { super(); mList = Collections.emptyList(); if (list != null) { mList = list; } mList2 = Collections.emptyList(); if (list2 != null) { mList2 = list2; } mMapper = mapper == null ? this : mapper; mIndexMapper = indexMapper == null ? this : indexMapper; mFilter = this; mFilter2 = this; mCache = new LruCache<Integer, E>(1000); }
public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected if (cache == null) { final int memClass = ((ActivityManager) imageView.getContext().getSystemService( Context.ACTIVITY_SERVICE)).getMemoryClass(); cache = new LruCache<String, Bitmap>(1024 * 1024 * memClass / 3) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } if (diskCache == null) { try { diskCache = DiskLruCache.open(imageView.getContext().getCacheDir(), 1, 1, 1024 * 1024 * 10); } catch (IOException e) { e.printStackTrace(); } } weakReference = new WeakReference<>(imageView); }
private ThumbnailLoader(){ memoryCache = new LruCache<String, Bitmap>(2 * FinalConstants.byte_1m){ @Override protected int sizeOf(String key, Bitmap value) { return value.getWidth() * value.getHeight() * 4; } @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { super.entryRemoved(evicted, key, oldValue, newValue); } }; diskCache = DiskLruCache.openCache(App.app, new File(PathConstants.getThumbpath()), 16 * FinalConstants.byte_1m); }
public BaseContactsAdapter(Context context, List<T> list) { super(); this.context = context; this.list = list; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { if (AppFactory.isCompatible(12)) { return bitmap.getByteCount() / 1024; } else { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } } }; }
public SearchAdapter(Context context, List<CallLogInfo> callLogInfoList, List<ContactInfo> contactInfoList) { this.context = context; this.callLogInfoList = callLogInfoList; this.contactInfoList = contactInfoList; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { if (AppFactory.isCompatible(12)) { return bitmap.getByteCount() / 1024; } else { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } } }; }
public static List<CollectionDAO> query(String collectionName) { List<CollectionDAO> list = new ArrayList<>(); LruCache<String, CollectionDAO> cache = mCache.get(collectionName); if (cache != null && cache.size() > 0) { list.addAll(cache.snapshot().values()); return list; } Cursor cursor = DBManager.getInstance().query(TABLE_NAME, null, COLUMN_COLLECTION_NAME + "=?", new String[]{collectionName}); if (cursor != null) { if (cursor.getCount() > 0) { cursor.moveToFirst(); do { list.add(new CollectionDAO(cursor)); } while (cursor.moveToNext()); } cursor.close(); } return list; }
public static CollectionDAO query(String collectionName, String documentID) { CollectionDAO dao = null; LruCache<String, CollectionDAO> cache = mCache.get(collectionName); if (cache != null) { dao = cache.get(documentID); if (dao != null) { return dao; } } Cursor cursor = DBManager.getInstance().query(TABLE_NAME, null, COLUMN_COLLECTION_NAME + "=? AND " + COLUMN_DOCUMENT_ID + "=?", new String[]{collectionName, documentID}); if (cursor != null) { if (cursor.getCount() > 0) { cursor.moveToFirst(); do { dao = new CollectionDAO(cursor); } while (cursor.moveToNext()); } cursor.close(); } return dao; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private BitmapCache() { // Get memory class of this device, exceeding this amount will throw an // OutOfMemory exception. final ActivityManager am = ((ActivityManager) VLCApplication.getAppContext().getSystemService( Context.ACTIVITY_SERVICE)); final int memClass = AndroidUtil.isHoneycombOrLater() ? am.getLargeMemoryClass() : am.getMemoryClass(); // Use 1/5th of the available memory for this memory cache. final int cacheSize = 1024 * 1024 * memClass / 5; Log.d(TAG, "LRUCache size sets to " + cacheSize); mMemCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; }
/** * Obtains or creates a new memory cache to store the weather icons. */ private void setIconMemoryCache() { IconCacheRetainFragment retainFragment = IconCacheRetainFragment.findOrCreateRetainFragment(getSupportFragmentManager()); iconCache = retainFragment.iconCache; if (iconCache == null) { // maximum available VM memory, stored in kilobytes int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // we use 1/8th of the available memory for this memory cache int cacheSize = maxMemory / 8; iconCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { // the cache size will be measured in kilobytes rather than number of items return bitmap.getByteCount() / 1024; } }; } retainFragment.iconCache = iconCache; }
private NativeImageLoader() { //获取应用程序的最大内存 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); //用最大内存的1/4来存储图片 final int cacheSize = maxMemory / 4; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { //获取每张图片的大小 @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } }; }