Java Code Examples for android.app.ActivityManager#getMemoryInfo()

The following examples show how to use android.app.ActivityManager#getMemoryInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SystemWatcher.java    From cube-sdk with Apache License 2.0 6 votes vote down vote up
public void run() {
    mTimerTask = new TimerTask() {

        @Override
        public void run() {

            MemoryInfo mi = new MemoryInfo();
            ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
            activityManager.getMemoryInfo(mi);
            Runtime runtime = Runtime.getRuntime();
            String s = String.format("free:%s%% %sKB total:%sKB max:%sKB ", runtime.freeMemory() * 100f / runtime.totalMemory(), runtime.freeMemory(), runtime.totalMemory() / 1024,
                    runtime.maxMemory() / 1024);
            // s += String.format("native: free:%sKB total:%sKB max:%sKB", android.os.Debug.getNativeHeapFreeSize() / 1024, android.os.Debug.getNativeHeapAllocatedSize() / 1024,
            // android.os.Debug.getNativeHeapSize() / 1024);
            // s += String.format("| availMem:%sKB", mi.availMem / 1024);
            Log.d("memory", s);
        }
    };

    mTimer = new Timer();
    mTimer.schedule(mTimerTask, 1000, 1000);
}
 
Example 2
Source File: WXUtils.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
public static long getAvailMemory(Context context){
  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
  am.getMemoryInfo(mi);
  //mi.availMem; 当前系统的可用内存
  //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
  WXLogUtils.w("app AvailMemory ---->>>"+mi.availMem/(1024*1024));
  return mi.availMem/(1024*1024);
}
 
Example 3
Source File: MemoryUtil.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
/**
 * 同步获取系统的总ram大小
 *
 * @param activityManager
 * @return
 */
private static long getRamTotalMem(ActivityManager activityManager) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(mi);
        return mi.totalMem / 1024;
    } else if (sTotalMem.get() > 0L) {//如果已经从文件获取过值,则不需要再次获取
        return sTotalMem.get();
    } else {
        final long tm = getRamTotalMemByFile();
        sTotalMem.set(tm);
        return tm;
    }
}
 
Example 4
Source File: MemInfo.java    From react-native-sunmi-inner-printer with MIT License 5 votes vote down vote up
public static long getmem_UNUSED(Context mContext) {
    long MEM_UNUSED;
    // 得到ActivityManager
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);	
    // 创建ActivityManager.MemoryInfo对象          
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(mi);
    // 取得剩余的内存空间        
    MEM_UNUSED = mi.availMem/1048576;
    return MEM_UNUSED;
}
 
Example 5
Source File: MemoryInfo.java    From MobileInfo with Apache License 2.0 5 votes vote down vote up
/**
 * 获取android当前可用内存大小
 *
 * @param context
 * @return
 */
private static String getAvailMemory(Context context) {

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    if (am != null) {
        am.getMemoryInfo(mi);
    }
    //mi.availMem; 当前系统的可用内存
    return Formatter.formatFileSize(context, mi.availMem);
}
 
Example 6
Source File: FileSizeUtil.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前可用内存,返回数据以字节为单位。
 *
 * @param context 可传入应用程序上下文。
 * @return 当前可用内存单位为B。
 */
public static long getAvailableMemory(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(memoryInfo);
    return memoryInfo.availMem;
}
 
Example 7
Source File: SkiaPooledImageRegionDecoder.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
private boolean isLowMemory() {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    if (activityManager != null) {
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.lowMemory;
    } else {
        return true;
    }
}
 
Example 8
Source File: DeviceInfoUtil.java    From FloatUtil with MIT License 5 votes vote down vote up
public static long getFreeMemory(Context context) {

        // 获取android当前可用内存大小
        long fm = 0;
        try {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
            am.getMemoryInfo(mi);
            fm = mi.availMem/(1024*1024);
        } catch (Exception e) {

        }

        return fm;
    }
 
Example 9
Source File: SkiaPooledImageRegionDecoder.java    From BlogDemo with Apache License 2.0 5 votes vote down vote up
private boolean isLowMemory() {
    ActivityManager activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
    if (activityManager != null) {
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.lowMemory;
    } else {
        return true;
    }
}
 
Example 10
Source File: DeviceUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
public static String getRamInfo(Context context) {
    long totalSize;
    long availableSize;

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);

    totalSize = memoryInfo.totalMem;
    availableSize = memoryInfo.availMem;

    return String.format("%s / %s", Formatter.formatFileSize(context, availableSize), Formatter.formatFileSize(context, totalSize));
}
 
Example 11
Source File: SystemTool.java    From KJFrameForAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * 获取设备的可用内存大小
 * 
 * @param cxt
 *            应用上下文对象context
 * @return 当前内存大小
 */
public static int getDeviceUsableMemory(Context cxt) {
    ActivityManager am = (ActivityManager) cxt
            .getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);
    // 返回当前系统的可用内存
    return (int) (mi.availMem / (1024 * 1024));
}
 
Example 12
Source File: SkiaPooledImageRegionDecoder.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private boolean isLowMemory() {
    ActivityManager activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
    if (activityManager != null) {
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.lowMemory;
    } else {
        return true;
    }
}
 
Example 13
Source File: DeviceUtils.java    From BookReader with Apache License 2.0 5 votes vote down vote up
/**
 * 获取系统当前可用内存大小
 *
 * @param context
 * @return
 */
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getAvailMemory(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(mi);
    return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
}
 
Example 14
Source File: MemoryTools.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
public static Long getTotalMemory(Context cx) {// 获取android全部内存大小
	if (cx == null) {
		return 0L;
	}

	ActivityManager am = (ActivityManager) cx.getSystemService(Context.ACTIVITY_SERVICE);
	MemoryInfo mi = new MemoryInfo();
	am.getMemoryInfo(mi);
	LogUtil.i(TAG, "Total memory: " + mi.totalMem);
	// mi.totalMem; 当前系统的全部内存

	return mi.totalMem / BYTES_PER_MEGA;// 将获取的内存大小规格化
}
 
Example 15
Source File: MemoryUtil.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * Get available memory info.
 */
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getAvailMemory(Context context) {// 获取android当前可用内存大小
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(mi);
    // mi.availMem; 当前系统的可用内存
    return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
}
 
Example 16
Source File: SystemTool.java    From Common with Apache License 2.0 5 votes vote down vote up
/**
 * 获取设备的可用内存大小
 *
 * @param cxt 应用上下文对象context
 * @return 当前内存大小
 */
public static int getDeviceUsableMemory(Context cxt) {
    ActivityManager am = (ActivityManager) cxt
            .getSystemService(Context.ACTIVITY_SERVICE);
    MemoryInfo mi = new MemoryInfo();
    am.getMemoryInfo(mi);
    // 返回当前系统的可用内存
    return (int) (mi.availMem / (1024 * 1024));
}
 
Example 17
Source File: CompatibleApps.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private static void checkMemoryConstraints() {
    final ActivityManager actManager = (ActivityManager) xdrip.getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
    final ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
    actManager.getMemoryInfo(memInfo);
    final long totalMemory = memInfo.totalMem;
    // TODO react to total memory
}
 
Example 18
Source File: WXUtils.java    From weex-uikit with MIT License 5 votes vote down vote up
public static long getAvailMemory(Context context){
  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
  am.getMemoryInfo(mi);
  //mi.availMem; 当前系统的可用内存
  //return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
  WXLogUtils.w("app AvailMemory ---->>>"+mi.availMem/(1024*1024));
  return mi.availMem/(1024*1024);
}
 
Example 19
Source File: PerformanceUtils.java    From AndroidPerformanceMonitor with Apache License 2.0 4 votes vote down vote up
public static long getFreeMemory() {
    ActivityManager am = (ActivityManager) BlockCanaryInternals.getContext().provideContext().getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    am.getMemoryInfo(mi);
    return mi.availMem / 1024;
}
 
Example 20
Source File: MemoryInfoUtil.java    From letv with Apache License 2.0 4 votes vote down vote up
public static long getMemUnused(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService("activity");
    MemoryInfo memoryInfo = new MemoryInfo();
    am.getMemoryInfo(memoryInfo);
    return memoryInfo.availMem / 1024;
}