2

我正在使用它来获取进程占用的内存,请帮助我,我想显示所有其他已安装应用程序占用的内存并希望在图表中显示

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActivityManager activityManager = (ActivityManager) this
            .getSystemService(ACTIVITY_SERVICE);
    MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);

    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");
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

4

2 回答 2

2
 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");
    }
于 2013-04-22T06:29:37.643 回答
0

您可以使用ChartDroid 之类的框架。还有其他框架可用于绘制图表/图形 - https://stackoverflow.com/questions/424752/any-good-graphing-packages-for-android

如果您想要针对此问题的现成解决方案,请查看Norton Utilities应用程序。

于 2013-03-19T12:02:11.507 回答