下面列出了怎么用android.os.Debug.MemoryInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 获取进程内存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;
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 获取进程内存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 MemoryInfo getMemoryInfo() {
/* Lazy allocation. */
if (SystemUtils.sMemoryInfo == null) {
SystemUtils.sMemoryInfo = new MemoryInfo();
}
Debug.getMemoryInfo(SystemUtils.sMemoryInfo);
return SystemUtils.sMemoryInfo;
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* Get system memory status.
*
* <p>Info taken from: http://huenlil.pixnet.net/blog/post/26872625
*
* @param context the context to use.
* @return a description of the current status.
*/
@SuppressWarnings("nls")
public static String getMemoryStatus(Context context) {
MemoryInfo memoryInfo = new 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);
if (context != null) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = activityManager.getMemoryClass();
memMessage = memMessage + "\nMemoryClass: " + memoryClass;
try {
Method getLargeMemoryMethod = ActivityManager.class.getMethod("getLargeMemoryClass");
int largeMemoryClass = (Integer) getLargeMemoryMethod.invoke(activityManager, (Object[]) null);
memMessage = memMessage + "\nLargeMemoryClass: " + largeMemoryClass;
} catch (Exception e1) {
// ignore
}
}
return memMessage;
}