/** * Record an "begin" memory trace event. * Begin trace events should have a matching end event. */ @VisibleForTesting public static synchronized void begin(String name, MemoryInfo memoryInfo) { final long eventId = name.hashCode(); TraceEvent.startAsync(name, eventId); if (sEnabled && matchesFilter(name)) { // Done before calculating the starting perf data to ensure calculating the memory usage // does not influence the timing data. long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000; savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START, timestampUs, memoryInfo); if (sTrackTiming) { savePerfString(name, eventId, EventType.START, false); } } }
/** * Record an "end" memory trace event, to match a begin event. The * memory usage delta between begin and end is usually interesting to * graph code. */ @VisibleForTesting public static synchronized void end(String name, MemoryInfo memoryInfo) { final long eventId = name.hashCode(); TraceEvent.finishAsync(name, eventId); if (sEnabled && matchesFilter(name)) { if (sTrackTiming) { savePerfString(name, eventId, EventType.FINISH, false); } // Done after calculating the instant perf data to ensure calculating the memory usage // does not influence the timing data. long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000; savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH, timestampUs, memoryInfo); } }
/** * Save a perf trace event as a JSON dict. The format mirrors a TraceEvent dict. * * @param name The trace data * @param id The id of the event * @param type the type of trace event (I, S, F) * @param timestampUs The time stamp at which this event was recorded * @param memoryInfo Memory details to be included in this perf string, null if * no memory details are to be included. */ private static void savePerfString(String name, long id, EventType type, long timestampUs, MemoryInfo memoryInfo) { try { JSONObject traceObj = new JSONObject(); traceObj.put("cat", "Java"); traceObj.put("ts", timestampUs); traceObj.put("ph", type); traceObj.put("name", name); traceObj.put("id", id); if (memoryInfo != null) { int pss = memoryInfo.nativePss + memoryInfo.dalvikPss + memoryInfo.otherPss; traceObj.put("mem", pss); } sPerfTraceStrings.put(traceObj); } catch (JSONException e) { throw new RuntimeException(e); } }
/** * 获取进程内存PSS数据 * * @param context Android上下文 * @param pid 进程ID * @return {nativePss,dalvikPss,TotalPss} */ public static long[] getPSS(Context context, int pid) { long[] value = new long[3]; // Natvie Dalvik Total if (pid >= 0) { int[] pids = new int[1]; pids[0] = pid; ActivityManager mAm = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids); MemoryInfo pidMemoryInfo = memoryInfoArray[0]; value[0] = pidMemoryInfo.nativePss; value[1] = pidMemoryInfo.dalvikPss; value[2] = pidMemoryInfo.getTotalPss(); } else { value[0] = 0; value[1] = 0; value[2] = 0; } return value; }
/** * 获取进程内存信息 * @param pid * @param info */ private static void getProcessMemInfo(int pid, ProcMemInfo info) { if (pid >= 0) { int[] pids = new int[1]; pids[0] = pid; ActivityManager mAm = (ActivityManager) WTApplication .getContext().getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids); MemoryInfo pidMemoryInfo = memoryInfoArray[0]; info.setNatived(pidMemoryInfo.nativePss); info.setDalvik(pidMemoryInfo.dalvikPss); info.setTotal(pidMemoryInfo.getTotalPss()); } }
public static rgv a(Debug.MemoryInfo paramMemoryInfo) { rgv localrgv = new rgv(); localrgv.a = Integer.valueOf(paramMemoryInfo.dalvikPss); localrgv.b = Integer.valueOf(paramMemoryInfo.nativePss); localrgv.c = Integer.valueOf(paramMemoryInfo.otherPss); localrgv.d = Integer.valueOf(paramMemoryInfo.dalvikPrivateDirty); localrgv.e = Integer.valueOf(paramMemoryInfo.nativePrivateDirty); localrgv.f = Integer.valueOf(paramMemoryInfo.otherPrivateDirty); if (Build.VERSION.SDK_INT >= 19) { localrgv.g = Integer.valueOf(paramMemoryInfo.getTotalPrivateClean()); localrgv.i = Integer.valueOf(paramMemoryInfo.getTotalSwappablePss()); } localrgv.h = Integer.valueOf(paramMemoryInfo.getTotalSharedDirty()); return localrgv; }
/** * getTotalPss * * @param context * @param processName * @return */ public static long getTotalPss(Context context, String processName) { ActivityManager activityMgr = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> list = activityMgr.getRunningAppProcesses(); if (list != null) { for (RunningAppProcessInfo processInfo : list) { if (processInfo.processName.equals(processName)) { int pid = processInfo.pid; MemoryInfo[] memoryInfos = activityMgr .getProcessMemoryInfo(new int[] { pid }); MemoryInfo memoryInfo = memoryInfos[0]; int totalPss = memoryInfo.getTotalPss(); return totalPss; } } } return -1; }
@Override protected void onStart() { ActivityManager am = (ActivityManager)getContext().getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); ArrayList<RunningAppProcessInfo> runningProcesses = new ArrayList<RunningAppProcessInfo>(am.getRunningAppProcesses()); final int numProcesses = runningProcesses.size(); int[] runningProcessIds = new int[numProcesses]; for (int i=0; i<numProcesses; i++ ) { runningProcessIds[i] = runningProcesses.get(i).pid; } List<MemoryInfo> memoryInfos = Arrays.asList(am.getProcessMemoryInfo(runningProcessIds)); List<ProcessErrorStateInfo> errorInfos = am.getProcessesInErrorState(); Gson gson = getGson(); JsonObject data = new JsonObject(); data.add("RUNNING_PROCESS_INFO", gson.toJsonTree(runningProcesses)); data.add("RUNNING_PROCESS_MEMORY_INFO", gson.toJsonTree(memoryInfos)); data.add("ERRORED_PROCESS_INFO", gson.toJsonTree(errorInfos)); data.add("CPU_LOAD", gson.toJsonTree(getCpuLoad())); data.add("MEM_INFO", gson.toJsonTree(getMemInfo())); data.add("NET_DEV", gson.toJsonTree(getNetDev())); sendData(data); stop(); }
public static void printDeviceInformation(Context ctx, PrintWriter out) { out.println(); out.println("Memory Information --------"); MemoryInfo mem = new MemoryInfo(); Debug.getMemoryInfo(mem); out.println("TotalPss: " + mem.getTotalPss() + "KB"); out.println("TotalPrivateDirty: " + mem.getTotalPrivateDirty() + "KB"); out.println("TotalSharedDirty: " + mem.getTotalSharedDirty() + "KB"); out.println("HeapAllocatedSize: " + Debug.getNativeHeapAllocatedSize() / 1024 + "KB"); out.println("HeapSize: " + Debug.getNativeHeapSize() / 1024 + "KB"); out.println(); out.println("Device Information ---------"); out.println("manufactor: " + Build.MANUFACTURER); out.println("model: " + Build.MODEL); out.println("version: " + Build.VERSION.RELEASE); out.println("product: " + Build.PRODUCT); out.println("hardware: " + Build.HARDWARE); out.println("board: " + Build.BOARD); out.println("device: " + Build.DEVICE); out.println("CPU_ABI: " + Build.CPU_ABI); out.println("CPU_ABI2: " + Build.CPU_ABI2); out.println(); out.println("Display Information --------"); DisplayMetrics dm = ctx.getResources().getDisplayMetrics(); out.println("Width: " + dm.widthPixels); out.println("Height: " + dm.heightPixels); out.println("Density: " + dm.density); out.println("DPI: " + dm.densityDpi); out.println("ScaledDensity: " + dm.scaledDensity); }
/** * 获取进程内存Private Dirty数据 * * @param context Android上下文 * @param pid 进程ID * @return {nativePrivateDirty,dalvikPrivateDirty,TotalPrivateDirty} */ public static long[] getPrivDirty(Context context, int pid) { ActivityManager mAm = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); int[] pids = new int[1]; pids[0] = pid; MemoryInfo[] memoryInfoArray = mAm.getProcessMemoryInfo(pids); MemoryInfo pidMemoryInfo = memoryInfoArray[0]; long[] value = new long[3]; // Natvie Dalvik Total value[0] = pidMemoryInfo.nativePrivateDirty; value[1] = pidMemoryInfo.dalvikPrivateDirty; value[2] = pidMemoryInfo.getTotalPrivateDirty(); return value; }
/** * 获取可用内存 * */ public static long getAvailableMemory(Context context) { ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(mi); return mi.availMem; }
/** * 得到设备的可用RAM * @return 返回所有内存大小,单位:kb */ private long getAvailMemory(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return mi.availMem / 1024; }
public static MemoryInfo getMemoryInfo() { /* Lazy allocation. */ if (SystemUtils.sMemoryInfo == null) { SystemUtils.sMemoryInfo = new MemoryInfo(); } Debug.getMemoryInfo(SystemUtils.sMemoryInfo); return SystemUtils.sMemoryInfo; }
/** * ��ȡ�ֻ��Ľ�����Ϣ * @param context * @return */ public static List<TaskInfo> getTaskprovider(Context context) { List<TaskInfo> taskInfos = new ArrayList<TaskInfo>(); ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE); PackageManager pm = context.getPackageManager(); List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses(); for (RunningAppProcessInfo processInfo : processInfos) { TaskInfo taskInfo = new TaskInfo(); String packageName = processInfo.processName; taskInfo.setPackname(packageName); MemoryInfo[] mi = am.getProcessMemoryInfo(new int[]{processInfo.pid}); taskInfo.setMemSize(mi[0].getTotalPrivateDirty()*1024); try { ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0); taskInfo.setName(applicationInfo.loadLabel(pm).toString()); taskInfo.setIcon(applicationInfo.loadIcon(pm)); if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { taskInfo.setUserTack(true); } else { taskInfo.setUserTack(false); } } catch (NameNotFoundException e) { e.printStackTrace(); /** * �Ҳ����İ�������android.process.acore; android.process.media; * ��������c����д�ģ�Ϊ�������Ĭ�ϵ���Ϣ */ taskInfo.setIcon(context.getResources().getDrawable(R.drawable.ic_launcher)); taskInfo.setName(packageName); } taskInfos.add(taskInfo); } return taskInfos; }
public static List<AppInfo> getRunningApplication(Context context) { MemoryInfo processMemoryInfo; PackageManager pm = context.getPackageManager(); ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> runningAppProcesses = am .getRunningAppProcesses(); List<AppInfo> list = new ArrayList<AppInfo>(); for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { AppInfo info = new AppInfo(); processMemoryInfo = am .getProcessMemoryInfo(new int[] { runningAppProcessInfo.pid })[0]; long mem = processMemoryInfo.getTotalPrivateDirty() * 1024L; info.mem = mem; info.packagename = runningAppProcessInfo.processName; try { ApplicationInfo applicationInfo = pm.getApplicationInfo( runningAppProcessInfo.processName, 0); info.sysApp = ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) || ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0); info.icon = applicationInfo.loadIcon(pm); info.name = applicationInfo.loadLabel(pm).toString(); list.add(info); } catch (NameNotFoundException e) { e.printStackTrace(); info = null; } } return list; }
/** * 获取所有的进程信息 * @param context * @return */ public static List<TaskInfo> getTaskInfos(Context context) { ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); PackageManager pm = context.getPackageManager(); List<RunningAppProcessInfo> processInfos = am.getRunningAppProcesses(); List<TaskInfo> taskInfos = new ArrayList<TaskInfo>(); for (RunningAppProcessInfo info : processInfos) { TaskInfo taskInfo = new TaskInfo(); String packname = info.processName; taskInfo.setPackname(packname); MemoryInfo[] memoryInfos = am.getProcessMemoryInfo(new int[]{info.pid}); long memsize = memoryInfos[0].getTotalPrivateDirty() * 1024; taskInfo.setMemsize(memsize); try { ApplicationInfo applicationInfo = pm.getApplicationInfo(packname, 0); Drawable icon = applicationInfo.loadIcon(pm); taskInfo.setIcon(icon); String name = applicationInfo.loadLabel(pm).toString(); taskInfo.setName(name); if ((applicationInfo.flags &ApplicationInfo.FLAG_SYSTEM )== 0) { //用户进程 taskInfo.setUserTask(true); }else{//系统进程 taskInfo.setUserTask(false); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); taskInfo.setIcon(context.getResources().getDrawable(R.drawable.ic_default)); taskInfo.setName(packname); } taskInfos.add(taskInfo); } return taskInfos; }
/** * Get system memory status. * * <p>Info taken from: http://huenlil.pixnet.net/blog/post/26872625 * * @return a description of the current status. */ public static String getMemoryStatus() { MemoryInfo memoryInfo = new android.os.Debug.MemoryInfo(); android.os.Debug.getMemoryInfo(memoryInfo); /* * The Pss number is a metric the kernel computes that takes into * account memory sharing -- basically each page of RAM in a process * is scaled by a ratio of the number of other processes also using * that page. This way you can (in theory) add up the pss across all * processes to see the total RAM they are using, and compare pss between * processes to get a rough idea of their relative weight. */ double totalPss = memoryInfo.getTotalPss() / 1024.0; /* * The other interesting metric here is PrivateDirty, which is basically * the amount of RAM inside the process that can not be paged to disk * (it is not backed by the same data on disk), and is not shared with * any other processes. Another way to look at this is the RAM that will * become available to the system when that process goes away (and probably * quickly subsumed into caches and other uses of it). */ double totalPrivateDirty = memoryInfo.getTotalPrivateDirty() / 1024.0; double totalSharedDirty = memoryInfo.getTotalSharedDirty() / 1024.0; String memMessage = String.format("Memory Pss=%.2f MB\nMemory Private=%.2f MB\nMemory Shared=%.2f MB", totalPss, totalPrivateDirty, totalSharedDirty); return memMessage; }
public void snapCurrentMeminfo() { LogHelper.i("TAG", "snapCurrentMeminfo"); List<ActivityManager.RunningAppProcessInfo> processes = mActivityManager .getRunningAppProcesses(); final int NP = processes != null ? processes.size() : 0; int[] pids = new int[NP]; for (int i = 0; i < NP; i++) { ActivityManager.RunningAppProcessInfo pi = processes.get(i); pids[i] = pi.pid; } MemoryInfo[] mem = mActivityManager.getProcessMemoryInfo(pids); long avaMem = getTotalUsedSize(); long snaptime = System.currentTimeMillis(); SQLiteDatabase db = mdbHelper.getWritableDatabase(); // put the total used memory ContentValues values = new ContentValues(); values.put(MemDbHelper.TABLE_CLOUMN_NAME, MemDbHelper.TABLE_ROW_SYSTEM); values.put(MemDbHelper.TABLE_CLOUMN_TIME, snaptime); values.put(MemDbHelper.TABLE_CLOUMN_MEM, avaMem); db.insert(MemDbHelper.TABLE_NAME, null, values); for (int i = 0; i < NP; i++) { ActivityManager.RunningAppProcessInfo info = processes.get(i); MemoryInfo memInfo = mem[i]; LogHelper.i("TAG", "" + info.processName + " " + memInfo.getTotalPss()); values.put(MemDbHelper.TABLE_CLOUMN_NAME, info.processName); values.put(MemDbHelper.TABLE_CLOUMN_TIME, snaptime); values.put(MemDbHelper.TABLE_CLOUMN_MEM, memInfo.getTotalPss()); db.insert(MemDbHelper.TABLE_NAME, null, values); } db.close(); }
/** * Record an "begin" memory trace event. * Begin trace events should have a matching end event. */ public static synchronized void begin(String name, MemoryInfo memoryInfo) { final long eventId = name.hashCode(); TraceEvent.startAsync(name, eventId); if (sEnabled && matchesFilter(name)) { // Done before calculating the starting perf data to ensure calculating the memory usage // does not influence the timing data. long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000; savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START, timestampUs, memoryInfo); if (sTrackTiming) { savePerfString(name, eventId, EventType.START, false); } } }
/** * Record an "end" memory trace event, to match a begin event. The * memory usage delta between begin and end is usually interesting to * graph code. */ public static synchronized void end(String name, MemoryInfo memoryInfo) { final long eventId = name.hashCode(); TraceEvent.finishAsync(name, eventId); if (sEnabled && matchesFilter(name)) { if (sTrackTiming) { savePerfString(name, eventId, EventType.FINISH, false); } // Done after calculating the instant perf data to ensure calculating the memory usage // does not influence the timing data. long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000; savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH, timestampUs, memoryInfo); } }
@TargetApi(23) private static void maybeAddApi23MemoryStats(Text text, MemoryInfo memoryInfo) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return; } for (Entry<String, String> entry : memoryInfo.getMemoryStats().entrySet()) { text.addNL(entry.getKey() + ": " + entry.getValue()); } }