gpt4 book ai didi

java - Android内存使用显示错误信息

转载 作者:太空宇宙 更新时间:2023-11-04 14:09:30 24 4
gpt4 key购买 nike

有人知道如何使用 C++ 查找 Android 应用程序的内存使用情况,因为我无法使用 Java 获得正确的结果。我知道如何找到总内存的两种方法。首先是:

    //Total Memory
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);

public static String formatSize(long size) {
String suffix = null;
Long l = new Long(size);
double sizeD = l.doubleValue();

if (sizeD >= 1024) {
suffix = "KB";
sizeD /= 1024;
if (sizeD >= 1024) {
suffix = "MB";
sizeD /= 1024;
if (sizeD > 1024){
sizeD /= 1024;
suffix = "GB";
}
}
}

//size = (long)sizeD;


//return roundDouble(sizeD, 10) + suffix;


StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}

if (suffix != null) resultBuffer.append(suffix);
return resultBuffer.toString();


}

第二个是:

public long InternalTotalMemory(){
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long blockCount = statFs.getBlockCountLong();
long blockSize = statFs.getBlockSizeLong();
long total = blockCount * blockSize;
return total;
}

public static String BytesLongToString(long size){
double KB = 1 * 1024;
double MB = KB * 1024;
double GB = MB * 1024;
double TB = GB * 1024;

String memorySize = "";

if (size < KB){
memorySize = floatForm(size) + " byte";
} else if (size >= KB && size < MB){
memorySize = floatForm((double)size / KB) + " KB";
} else if (size >= MB && size < GB){
memorySize = floatForm((double)size / MB) + " MB";
} else if (size >= GB && size < TB){
memorySize = floatForm((double)size / GB) + " GB";
}
return memorySize;
}

public static String floatForm (double d)
{
return new DecimalFormat("#.###").format(d);
}

我不明白为什么两个函数结果不同或者其中一个格式错误?

最佳答案

Android 是 Linux,因此您可以使用 /proc/$PID/maps ( see here ) 等内容。

示例来自here :

void show_mappings(void)
{
DLOG("-----------------------------------------------\n");
int a;
FILE *f = fopen("/proc/self/maps", "r");
struct buffer* b = _new_buffer(1024);
while ((a = fgetc(f)) >= 0) {
if (_buf_putchar(b,a) || a == '\n') {
DLOG("/proc/self/maps: %s",_buf_reset(b));
}
}
if (b->pos) {
DLOG("/proc/self/maps: %s",_buf_reset(b));
}
free(b);
fclose(f);
DLOG("-----------------------------------------------\n");
}

(该文件的目的是了解内存地址,找出内存保护属性,请参阅同一个file中的read_mprotection(void* addr)。)

关于java - Android内存使用显示错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28497581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com