Skip to content Skip to sidebar Skip to footer

How To Find Memory Usage Of My Android Application Written C++ Using Ndk

I am porting a game written in C++ to Android using NDK. I need to know how much memory it consumes while running. I am looking for programmatically way to find the memory usage of

Solution 1:

The two functions based on JonnyBoy's answer.

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

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

Solution 2:

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

Solution 3:

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

Post a Comment for "How To Find Memory Usage Of My Android Application Written C++ Using Ndk"