0

我正在尝试获取有关物理内存的信息,例如总内存、已用内存和可用内存,而不仅仅是关于堆的信息。

我在这里搜索过这样的问题,但没有找到正确的答案。

我试过这样:

long freeSize = 0L;
    long totalSize = 0L;
    long usedSize = 0L;


    try {
        Runtime info = Runtime.getRuntime();
        freeSize = info.freeMemory();
        totalSize = info.totalMemory();
        usedSize = totalSize - freeSize;
    } catch (Exception e) {
        e.printStackTrace();
    }

但是不要获取正确的信息。

我也尝试过 MemoryInfo 类。

谢谢!

4

3 回答 3

0

另一种计算当前正在运行的应用程序的内存使用情况的方法。

public static long getUsedMemorySize() {

    long freeSize = 0L;
    long totalSize = 0L;
    long usedSize = -1L;
    try {
        Runtime info = Runtime.getRuntime();
        freeSize = info.freeMemory();
        totalSize = info.totalMemory();
        usedSize = totalSize - freeSize;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return usedSize;

}
于 2012-12-06T12:21:48.277 回答
0
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
            MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            activityManager.getMemoryInfo(memoryInfo);

            String TAG = "a";
            Log.i(TAG, " memoryInfo.availMem " + memoryInfo.availMem + "\n" );
            Log.i(TAG, " memoryInfo.lowMemory " + memoryInfo.lowMemory + "\n" );
            Log.i(TAG, " memoryInfo.threshold " + memoryInfo.threshold + "\n" );

            List<RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();

            Map<Integer, String> pidMap = new TreeMap<Integer, String>();
            for (RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses)
            {
                pidMap.put(runningAppProcessInfo.pid, runningAppProcessInfo.processName);
            }

            Collection<Integer> keys = pidMap.keySet();

            for(int key : keys)
            {
                int pids[] = new int[1];
                pids[0] = key;
                android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
                for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray)
                {
                    Log.i(TAG, String.format("** MEMINFO in pid %d [%s] **\n",pids[0],pidMap.get(pids[0])));
                    Log.i(TAG, " pidMemoryInfo.getTotalPrivateDirty(): " + pidMemoryInfo.getTotalPrivateDirty() + "\n");
                    Log.i(TAG, " pidMemoryInfo.getTotalPss(): " + pidMemoryInfo.getTotalPss() + "\n");
                    Log.i(TAG, " pidMemoryInfo.getTotalSharedDirty(): " + pidMemoryInfo.getTotalSharedDirty() + "\n");
                }
            }
于 2012-12-07T08:38:51.937 回答
0

获取当前内存:

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
于 2012-12-06T12:16:08.390 回答