android.os.Debug.MemoryInfo Java Examples

The following examples show how to use android.os.Debug.MemoryInfo. 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: MEMUtils.java    From GTTools with MIT License 6 votes vote down vote up
/**
 * 获取进程内存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;
}
 
Example #2
Source File: PerfTraceEvent.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #3
Source File: PerfTraceEvent.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #4
Source File: PerfTraceEvent.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #5
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #6
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #7
Source File: MEMUtils.java    From GTTools with MIT License 5 votes vote down vote up
/**
 * 获取进程内存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;
}
 
Example #8
Source File: SystemUtils.java    From tilt-game-android with MIT License 5 votes vote down vote up
public static MemoryInfo getMemoryInfo() {
	/* Lazy allocation. */
	if (SystemUtils.sMemoryInfo == null) {
		SystemUtils.sMemoryInfo = new MemoryInfo();
	}

	Debug.getMemoryInfo(SystemUtils.sMemoryInfo);

	return SystemUtils.sMemoryInfo;
}
 
Example #9
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #10
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #11
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #12
Source File: PerfTraceEvent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #13
Source File: MemoryUtilities.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}