安卓 - 如何获取我的安卓设备的内存使用情况

4
你好,希望你能过得愉快。我的朋友们,我想知道我的安卓设备有多少内存以及使用了多少内存,包括活动内存、非活动内存、空闲内存和有线内存的大小(单位为MB)。我已经通过这个链接获悉通过解析“/system/bin/cat”、“/proc/meminfo”可以获取上述信息,是的,我成功获取了这些信息,但是我收到了一个很长的字符串,其中包含了所有东西。
 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

我从这里获取到的字符串比之前更长

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

我想知道是否可以动态地将此字符串拆分为不同的设备,或者是否有其他方法来找出上述内容。
我还查阅了这个,但它也没有太多用处……我能轻松获取可用内存信息,但我需要全部四个。
非常感谢任何帮助。提前致谢。

看这个...https://dev59.com/63E95IYBdhLWcg3wd9tK - Mehul
1个回答

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

请参考如何在Android中发现我的应用程序的内存使用情况?

这就是你需要的全部信息 ;)


首先非常感谢Thomas K,这将给出总内存使用情况,但我想知道活动内存使用情况、非活动使用情况、空闲内存和有线内存,所以从这里我可以获得空闲内存,我能够使用活动管理器来获取它,然后所有三个告诉关于内存使用情况pidMemoryInfo.getTotalPrivateDirty(): 3824 pidMemoryInfo.getTotalPss(): 4858 pidMemoryInfo.getTotalSharedDirty(): 10292。 - user1346836
你能帮我看看这段代码或以上的代码,如何获取这四个东西吗?我一直在尝试,但实际上无法找到解决方案。如果你能帮我解决这个问题,那将非常有帮助。 - user1346836

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接