1

does anybody know how to find android apps memory usage using C++, because I can not get correct result using Java. I know two way how to find total Internal memory. First is:

    //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();


}

Second is:

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

I don't understand why two functions result is different or one of the format is wrong?

qazxsw123
  • 11
  • 1

1 Answers1

0

Android is a Linux, so you can use stuff like /proc/$PID/maps (see here).

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

(The purpose of the file was knowing memory address, find out memory protection attributes, see read_mprotection(void* addr) in the same file.)

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • Did you know how other apps which scan memory works, did they use native c++ libraries or get info from Linux? – qazxsw123 Feb 13 '15 at 10:49