Android系列之获取设备内存数据信息
程序员文章站
2022-06-23 13:37:41
App分配内存 首先,我们通过Android系统提供的方法获取 分配给App本身的运行数据信息,这里我们能获取系统分配给App的最大内存信息(这个数据根据不同的机型可能会不一样)ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);ActivityManager.MemoryInfo mi = new ActivityManager.MemoryI......
App分配内存
首先,我们通过Android系统提供的方法获取 分配给App本身的运行数据信息,这里我们能获取系统分配给App的最大内存信息(这个数据根据不同的机型可能会不一样),当前已分配内存大小,以及当前分配剩余内存大小等内存数据信息。
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
//设备内存
manager.getMemoryInfo(mi);
String totalMem = Formatter.formatFileSize(context, mi.totalMem);
System.out.println("设备的运行总内存 totalMem: " + totalMem);
String availMem = Formatter.formatFileSize(context, mi.availMem);
System.out.println("设备剩余运行内存 availMem: " + availMem);
//最大分配内存获取方法1
int memory = manager.getMemoryClass();
System.out.println("app可分配最大内存memory: " + memory);
//最大分配内存获取方法2
float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0 / (1024 * 1024));
System.out.println("app可分配最大内存 maxMemory: " + maxMemory);
//当前分配的总内存
float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0 / (1024 * 1024));
System.out.println("app当前分配内存totalMemory: " + totalMemory);
//当前分配使用剩余内存
float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0 / (1024 * 1024));
System.out.println("app当前分配剩余内存freeMemory: " + reeMemory);
设备运行内存
我们通过Android系统提供的方法获取设备的总运行内存数据信息,但是我们获取的可用内存信息可能不是很准确,可能会包含一些实际在运行的App所占内存信息。
manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
manager.getMemoryInfo(mi);
// double totalMem = (double) (mi.totalMem * 1.0 / (1024 * 1024));
String totalMem = Formatter.formatFileSize(context, mi.totalMem);
System.out.println(("设备的运行总内存: " + totalMem + " M"));
// double availMem= (double) (mi.availMem* 1.0 / (1024 * 1024));
String availMem = Formatter.formatFileSize(context, mi.availMem);
System.out.println(("设备剩余运行内存: " + availMem));
当然也有其他的方法获取内存信息,比如直接读取系统的内存信息保存文件 /proc/meminfo
直接读取此文件信息的方法如下
public static List<String> getMemInfo() {
List<String> result = new ArrayList<>();
try {
String line;
FileReader reader = new FileReader("/proc/meminfo");
BufferedReader br = new BufferedReader(reader);
while ((line = br.readLine()) != null) {
result.add(line);
}
reader.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/* /proc/meminfo
MemTotal: 2902436 kB
MemFree: 199240 kB
MemAvailable: 1088764 kB
Buffers: 40848 kB
Cached: 862908 kB
SwapCached: 54696 kB
Active: 1222848 kB
Inactive: 671468 kB
Active(anon): 758516 kB
Inactive(anon): 242560 kB
Active(file): 464332 kB
Inactive(file): 428908 kB
Unevictable: 5972 kB
Mlocked: 256 kB
SwapTotal: 1048572 kB
SwapFree: 537124 kB
Dirty: 12 kB
Writeback: 0 kB
AnonPages: 988820 kB
Mapped: 508996 kB
Shmem: 4800 kB
Slab: 157204 kB
SReclaimable: 57364 kB
SUnreclaim: 99840 kB
KernelStack: 41376 kB
PageTables: 51820 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2499788 kB
Committed_AS: 76292324 kB
VmallocTotal: 258867136 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
*/
获取指定的内存信息情况的方法
public static String getFieldFromMeminfo(String field) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/proc/meminfo"));
Pattern p = Pattern.compile(field + "\\s*:\\s*(.*)");
try {
String line;
while ((line = br.readLine()) != null) {
Matcher m = p.matcher(line);
if (m.matches()) {
return m.group(1);
}
}
} finally {
br.close();
}
return null;
}
public static String getMemTotal() {
String result = null;
try {
result = getFieldFromMeminfo("MemTotal");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String getMemFree() {
String result = null;
try {
result = getFieldFromMeminfo("MemFree");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String getMemAvailable() {
String result = null;
try {
result = getFieldFromMeminfo("MemAvailable");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
本文地址:https://blog.csdn.net/u014571139/article/details/112618672