How to find memory usage of my android application written C++ using NDK

99封情书 提交于 2019-12-03 15:10:21

In Java, you can check the native memory allocated/used with:

Debug.getNativeHeapAllocatedSize()
Debug.getNativeHeapSize()

See:

http://developer.android.com/reference/android/os/Debug.html#getNativeHeapAllocatedSize%28%29

http://developer.android.com/reference/android/os/Debug.html#getNativeHeapSize%28%29

The two functions based on JonnyBoy's answer.

static long getNativeHeapAllocatedSize(JNIEnv *env)
{
    jclass clazz = (*env)->FindClass(env, "android/os/Debug");
    if (clazz)
    {
        jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "getNativeHeapAllocatedSize", "()J");
        if (mid)
        {
            return (*env)->CallStaticLongMethod(env, clazz, mid);
        }
    }
    return -1L;
}

static long getNativeHeapSize(JNIEnv *env)
{
    jclass clazz = (*env)->FindClass(env, "android/os/Debug");
    if (clazz)
    {
        jmethodID mid = (*env)->GetStaticMethodID(env, clazz, "getNativeHeapSize", "()J");
        if (mid)
        {
            return (*env)->CallStaticLongMethod(env, clazz, mid);
        }
    }
    return -1L;
}

Debug.getNativeHeapAllocatedSize() andDebug.getNativeHeapSize() return information about memory allocations performed by malloc() and related functions only. You can easily parse /proc/self/statm from C++ and get the VmRSS metric.

See details here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!