myunuts's blog

プログラム関係をメモっています

メモリ関連のテストプログラム

Eclairのころに作ったテストプログラム。
EngビルドとUserビルドで出力される内容が違ったと思うけど、もう覚えていない。。。

備忘のために、コードを残しておく。

public class AppMemTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    private int[] pid ;
    private static final String TAG = new String("MemTest");

    @Override
    public void onResume(){
        super.onResume();
        pid = new int[1];
        pid[0] = Process.myPid();
        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;
            pids[0] =Process.myPid();
            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");
            }
        }


        MemoryInfo memoryInfo1 = new ActivityManager.MemoryInfo();

        Log.e(LOGTAG, " availMem " + memoryInfo1.availMem + ", lowMemory " + memoryInfo1.lowMemory + ", threshold " + memoryInfo1.threshold);

        android.os.Debug.MemoryInfo[] info = activityManager.getProcessMemoryInfo(pid);

        int total = info[0].getTotalPrivateDirty();

        Log.e(LOGTAG, "TotalPrivateDirty " + total);

        long free = Runtime.getRuntime().freeMemory();

        Log.e(LOGTAG, "freeMemory " + free);

        if (total >=LMT_BRW_MEM && free <= LMT_FREE_MEM) {
                        finish();
                    }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();

    Log.e(LOGTAG, "onLowMemory!!!!!");
    }

    private static int LMT_BRW_MEM = (120 * 1024);
    private static int LMT_FREE_MEM = (14 * 1024 * 1024);
    private static String LOGTAG = new String("XXXXX");

確か、ローメモリーの調査に使ったはず。