Android examples for Hardware:Memory
clear Memory and log result
//package com.java2s; import android.app.ActivityManager; import android.content.Context; import android.util.Log; import android.widget.Toast; import java.util.List; public class Main { private final static String TAG_SYSTEM_UTIL = "SystemUtil"; public static void clearMemory(Context context) { long beforemem = getAvailableMemory(context); Log.d(TAG_SYSTEM_UTIL, "before memory:" + beforemem); ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos; List<ActivityManager.RunningServiceInfo> runningServiceInfos; if (am != null) { runningAppProcessInfos = am.getRunningAppProcesses(); runningServiceInfos = am.getRunningServices(100); if (runningAppProcessInfos != null) { for (ActivityManager.RunningAppProcessInfo r : runningAppProcessInfos) { if (r.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) { String[] pkgList = r.pkgList; for (String p : pkgList) { Log.e(TAG_SYSTEM_UTIL, "be killed process:" + p); am.killBackgroundProcesses(p); }/*w w w . j ava 2s .c o m*/ } } } if (runningServiceInfos != null) { for (ActivityManager.RunningServiceInfo r : runningServiceInfos) { } } } long aftermem = getAvailableMemory(context); Toast.makeText(context, (aftermem - beforemem) + "MB", Toast.LENGTH_LONG).show(); } /** * get available memory * * @param context * @return available memory?MB? */ public static long getAvailableMemory(Context context) { ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); am.getMemoryInfo(memoryInfo); Log.d(TAG_SYSTEM_UTIL, "before clear the memory is " + memoryInfo.availMem / (1024 * 1024)); return memoryInfo.availMem / (1024 * 1024); } }