android 10 增加了libmeminfo.so, 用于内存信息的查询,主要通过解析/proc/meminfo文件
源代码位于 system/core/libmeminfo, 代码非常简单没什么好说的
// system/core/libmeminfo/include/meminfo/sysmeminfo.h
class SysMemInfo final {
// System or Global memory accounting
public:
static constexpr const char* kMemTotal = "MemTotal:";
static constexpr const char* kMemFree = "MemFree:";
static constexpr const char* kMemBuffers = "Buffers:";
static constexpr const char* kMemCached = "Cached:";
static constexpr const char* kMemShmem = "Shmem:";
static constexpr const char* kMemSlab = "Slab:";
static constexpr const char* kMemSReclaim = "SReclaimable:";
static constexpr const char* kMemSUnreclaim = "SUnreclaim:";
static constexpr const char* kMemSwapTotal = "SwapTotal:";
static constexpr const char* kMemSwapFree = "SwapFree:";
static constexpr const char* kMemMapped = "Mapped:";
static constexpr const char* kMemVmallocUsed = "VmallocUsed:";
static constexpr const char* kMemPageTables = "PageTables:";
static constexpr const char* kMemKernelStack = "KernelStack:";
static constexpr const char* bMemcgUsage="memory.usage_in_bytes";
static constexpr const char* bMemcgLimit="memory.limit_in_bytes";
static constexpr const char* bMemcgSwUsage="memory.memsw.usage_in_bytes";
static constexpr const char* bMemcgSwLimit="memory.memsw.limit_in_bytes";
static const std::vector<std::string> kDefaultSysMemInfoTags;
static const std::vector<std::string> kDefaultSysMemcgTags;
SysMemInfo() = default;
// Parse /proc/meminfo and read values that are needed
bool ReadMemInfo(const std::string& path = "/proc/meminfo");
bool ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
const std::string& path = "/proc/meminfo");
bool ReadMemInfo(std::vector<uint64_t>* out, const std::string& path = "/proc/meminfo");
//bool ReadMemcgInfo(const std::string& path = "/dev/memcg");
bool ReadMemcgInfo(std::vector<uint64_t>* out, const std::string& path = "/dev/memcg");
bool ReadMemcgInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
const std::string& path = "/dev/memcg");
....
}
native中使用方法:
// frameworks/base/core/jni/android_os_Debug.cpp
static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
std::vector<uint64_t> mem(MEMINFO_COUNT);
std::vector<std::string> tags(::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags);
tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:");
::android::meminfo::SysMemInfo smi;
if (!smi.ReadMemInfo(tags, &mem)) {
jniThrowRuntimeException(env, "SysMemInfo read failed");
return;
}
...
}
android中接口在 frameworks/base/core/java/com/android/internal/util/MemInfoReader.java中
java中使用,参考 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
MemInfoReader memInfo = new MemInfoReader();
memInfo.readMemInfo();
final long cachedKb = memInfo.getCachedSizeKb();
final long freeKb = memInfo.getFreeSizeKb();
final long zramKb = memInfo.getZramTotalSizeKb();
final long kernelKb = memInfo.getKernelUsedSizeKb();
标签:std,const,libmeminfo,char,constexpr,vector,static,Android
From: https://www.cnblogs.com/tangshunhui/p/16715480.html