4

how you friends are doing, hope for the best. friends i want to how how much memory my android device does have and how much memory is used active memory , inactive memory, free memory and wired memory in MB i have gone through this link which says about that by parsing "/system/bin/cat", "/proc/meminfo" we can get things which i mentioned above, and yes i am getting all the things but i am getting a long string where i have everything

 MemTotal:         377192 kB
  MemFree:           10448 kB
  Buffers:            1368 kB``
  Cached:            74276 kB
  SwapCached:            0 kB
  Active:           143116 kB
  Inactive:         177292 kB
  Active(anon):     118388 kB
  Inactive(anon):   137664 kB
  Active(file):      24728 kB
  Inactive(file):    39628 kB
    Unevictable:       10664 kB
    Mlocked:           10372 kB
    HighTotal:        190464 kB
    HighFree:            568 kB
    LowTotal:         186728 kB
    LowFree:            9880 kB
    SwapTotal:             0 kB
    SwapFree:              0 kB
Dirty:                 8 kB
Writeback:             0 kB
AnonPages:        255440 kB
Mapped:            45816 kB
Shmem:               624 kB
Slab:              10952 kB
SReclaimable:       2456 kB
SUnreclaim:         8496 kB

even bigger string this i am getting from

 private String ReadCPUinfo()
{
ProcessBuilder cmd;
StringBuffer strMemory = new StringBuffer();
//final ActivityManager activityManager =(ActivityManager) 
context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager actvityManager = (ActivityManager) this.getSystemService(  
ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
strMemory.append("Available Memory : ");
strMemory.append(mInfo.availMem/1048576L);
strMemory.append("\n");
strMemory.append("\n");
String result=strMemory.toString();
try{
String[] args = {"/system/bin/cat", "/proc/meminfo"};
cmd = new ProcessBuilder(args);
Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println("itthhe   ====  ---   >>>>    "+new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}

i want to whether i can break this string dynamically for diff. devices or is there any other way to find out above things

i have also gone through this but it was also not that use full .. i am easily able to get available memory info but i need all four

any help is deeply appreciated thanks in adavance

Community
  • 1
  • 1
user1346836
  • 147
  • 1
  • 3
  • 12
  • See this one...http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android – Strider Jun 07 '12 at 15:47

1 Answers1

7
ActivityManager activityManager = (ActivityManager) context.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");
    }
}

See How do I discover memory usage of my application in Android?

That's all you need ;)

Community
  • 1
  • 1
Thkru
  • 4,128
  • 2
  • 16
  • 36
  • first of all thank you very much Thomas K, this will give total memory usage right but i want to know active memory usage inactive usage free memory and wired memory so from here i can get free memory , that i am able to get using activity manager, then all three tells about the memory usage pidMemoryInfo.getTotalPrivateDirty(): 3824 pidMemoryInfo.getTotalPss(): 4858 pidMemoryInfo.getTotalSharedDirty(): 10292 – user1346836 Jun 07 '12 at 16:10
  • can you please help me how can i get there four things from this or above code .. i feel that we can get i am trying and trying , but actually not able to get to the solution, it would be very helpful if you can help me on this – user1346836 Jun 07 '12 at 16:11