下面列出了android.app.ActivityManager#getMemoryClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Enables or disables the disk cache for the life of the application. This method cannot be
* called more than once per lifetime of the app and should therefore be called only during
* application startup; subsequent calls have no effect on the disk cache state.
*
* Note that when enabling Piccaso cache indicators, you may still find that some images appear
* as though they've been loaded from disk. This will be true for any app-packaged drawable or
* bitmap resource that's placed by Picasso. (These are always "from disk" with or without the
* presence of a disk cache.) Similarly, it's possible to get green-tagged images when using
* Picasso to "load" manually pre-loaded BitmapDrawables.
*
* @param diskCacheEnabled
*/
public static void setConfiguration(Context context, boolean diskCacheEnabled, Integer cacheHeapPercent) {
try {
Picasso.Builder builder = new Picasso.Builder((ArcusApplication.getContext()));
if (diskCacheEnabled) {
builder.downloader(new OkHttp3Downloader(new OkHttpClient()));
}
if (cacheHeapPercent != null) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
int heapSize = (int) ((float)(1024 * 1024 * memoryClass) * ((float) cacheHeapPercent / 100.0));
builder.memoryCache(new LruCache(heapSize));
logger.debug("Setting Picasso in-memory LRU cache max size to {} bytes; {}% of heap sized {}", heapSize, cacheHeapPercent, 1024 * 1024 * memoryClass);
}
Picasso.setSingletonInstance(builder.build());
} catch (IllegalStateException e) {
logger.warn("Picasso setConfiguration() has already been called; ignoring request.");
}
}
public CachingAsyncImageLoader(Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(
Context.ACTIVITY_SERVICE);
int memoryClass = (am.getMemoryClass() * 1024 * 1024) / CACHE_DIVISOR; //basically, set the heap to be everything we can get
this.context = context;
this.cache = new LruCache<>(memoryClass);
}
public static long availableMemory() {
final Runtime runtime = Runtime.getRuntime();
final long used = runtime.totalMemory() - runtime.freeMemory();
final ActivityManager activityManager = (ActivityManager) sContext.
getSystemService(Context.ACTIVITY_SERVICE);
final long total = activityManager.getMemoryClass() * 1024 * 1024;
return total - used;
}
/**
* Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
* Default cache size = 1/8 of available app memory.
*/
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
if (memoryCacheSize == 0) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
if (hasHoneycomb() && isLargeHeap(context)) {
memoryClass = getLargeMemoryClass(am);
}
memoryCacheSize = 1024 * 1024 * memoryClass / 8;
}
return new LruMemoryCache(memoryCacheSize);
}
private void setOptions() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
PackageManager pm = getContext().getPackageManager();
int state = pm.getComponentEnabledSetting(new ComponentName(getContext(), ActivitySearch.class));
swExternalSearch.setChecked(state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
swShortcuts.setChecked(prefs.getBoolean("shortcuts", true));
swFts.setChecked(prefs.getBoolean("fts", false));
swEnglish.setChecked(prefs.getBoolean("english", false));
swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
swUpdates.setChecked(prefs.getBoolean("updates", true));
swUpdates.setVisibility(
Helper.isPlayStoreInstall() || !Helper.hasValidFingerprint(getContext())
? View.GONE : View.VISIBLE);
swExperiments.setChecked(prefs.getBoolean("experiments", false));
swQueries.setChecked(prefs.getInt("query_threads", 4) < 4);
swCrashReports.setChecked(prefs.getBoolean("crash_reports", false));
tvUuid.setText(prefs.getString("uuid", null));
swDebug.setChecked(prefs.getBoolean("debug", false));
swAuthSasl.setChecked(prefs.getBoolean("auth_sasl", true));
swCleanupAttachments.setChecked(prefs.getBoolean("cleanup_attachments", false));
tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors()));
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
int class_mb = am.getMemoryClass();
tvMemoryClass.setText(getString(R.string.title_advanced_memory_class, class_mb + " MB"));
tvStorageSpace.setText(getString(R.string.title_advanced_storage_space,
Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));
tvFingerprint.setText(Helper.getFingerprint(getContext()));
grpDebug.setVisibility(swDebug.isChecked() || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
}
static int calculateMemoryCacheSize(Context context) {
ActivityManager am = ContextCompat.getSystemService(context, ActivityManager.class);
boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
int memoryClass = largeHeap ? am.getLargeMemoryClass() : am.getMemoryClass();
// Target ~15% of the available heap.
return (int) (1024L * 1024L * memoryClass / 7);
}
static int calculateMemoryCacheSize(Context context) {
ActivityManager am = getService(context, ACTIVITY_SERVICE);
boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (largeHeap && SDK_INT >= HONEYCOMB) {
memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
}
// Target ~15% of the available heap.
return 1024 * 1024 * memoryClass / 7;
}
/**
* Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
* Default cache size = 1/8 of available app memory.
*/
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
if (memoryCacheSize == 0) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
if (hasHoneycomb() && isLargeHeap(context)) {
memoryClass = getLargeMemoryClass(am);
}
memoryCacheSize = 1024 * 1024 * memoryClass / 8;
}
return new LruMemoryCache(memoryCacheSize);
}
public static int getHeapSize(Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
boolean isLargeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
int memoryClass = am.getMemoryClass();
if (isLargeHeap && Utils.isHoneycombOrLater()) {
memoryClass = am.getLargeMemoryClass();
}
return 1024 * memoryClass;
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
@TargetApi(VERSION_CODES.KITKAT)
public static String getMemoryClass(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String lowMem = "";
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice()) {
lowMem = ", low-mem device";
}
return activityManager.getMemoryClass() + lowMem;
}
static int getAvailableMemorySize(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int ret = -1;
boolean largeHeap = false;
if (Build.VERSION.SDK_INT >= 11) {
largeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
if (largeHeap)
ret = am.getLargeMemoryClass() * 1024;
}
if (!largeHeap || Build.VERSION.SDK_INT < 11) {
ret = am.getMemoryClass() * 1024;
}
return ret / 5;
}
@Override
public void onCreate() {
super.onCreate();
if(!BuildConfig.DEBUG) {
AnalyticsManager.intialize(getApplicationContext());
}
sInstance = this;
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
mRoots = new RootsCache(this);
mRoots.updateAsync();
mSAFManager = new SAFManager(this);
mThumbnails = new ThumbnailCache(memoryClassBytes / 4);
final IntentFilter packageFilter = new IntentFilter();
packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
packageFilter.addDataScheme("package");
registerReceiver(mCacheReceiver, packageFilter);
final IntentFilter localeFilter = new IntentFilter();
localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(mCacheReceiver, localeFilter);
isTelevision = Utils.isTelevision(this);
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
@Override
public int calculateCacheSize(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
if (targetMemoryCacheSize >= MAX_SIZE) {
return MAX_SIZE;
}
return targetMemoryCacheSize;
}
/**
* Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
* Default cache size = 1/8 of available app memory.
*/
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
if (memoryCacheSize == 0) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
if (hasHoneycomb() && isLargeHeap(context)) {
memoryClass = getLargeMemoryClass(am);
}
memoryCacheSize = 1024 * 1024 * memoryClass / 8;
}
return new LruMemoryCache(memoryCacheSize);
}
public CrossbowImageCache onCreateImageCache() {
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
int memCacheSize = ((am.getMemoryClass() * 1024 * 1024) / 10);
return new DefaultImageCache(memCacheSize);
}
private static int getMaxSize(@Nullable ActivityManager activityManager) {
final int memoryClassBytes = activityManager != null ? activityManager.getMemoryClass() * 1024 * 1024 : 100;
final boolean isLowMemoryDevice = isLowMemoryDevice(activityManager);
return Math.round(memoryClassBytes
* (isLowMemoryDevice ? LOW_MEMORY_MAX_SIZE_MULTIPLIER : MAX_SIZE_MULTIPLIER));
}
/**
* 获取应用能够获取的max dalvik堆内存大小
* 和Runtime.getRuntime().maxMemory()一样
*
* @param context
* @return 单位M
*/
public static long getAppTotalDalvikHeapSize(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return manager.getMemoryClass();
}