Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Android application written in C++.

like image 620
chandra mohan Avatar asked Jun 14 '13 13:06

chandra mohan


2 Answers

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;
}
like image 89
Wiz Avatar answered Oct 07 '22 22:10

Wiz


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

like image 28
JonnyBoy Avatar answered Oct 07 '22 21:10

JonnyBoy