Monday, January 16, 2012

How to know My Android Application/Process killed due to low Memory?

Prerequisites :
   1) ICS(Ice cream sandwich) AOSP code - 4.0.3

These are the pointers to verify in native source code for the "low-memory" condition.

1) Detect "Out of Memory on" situation in the below functions

    Function Names : dvmMalloc() calls
                                 tryMalloc ()
    File Name          : Heap.cpp
    Path                   : /dalvik/vm/alloc

2) ActivityManager kill the process.

    Function Name   : requestFinishActivityLocked
    File Name           :
    Path                    : /frameworks/base/services/java/com/android/server/am

3) Report process is died.

    Function Name   : appDiedLocked
    File Name           :
    Path                    : frameworks/base/services/java/com/android/server/am/

Enable the Native logs in above functions, and from this you can confirm that the application is getting killed because of low memory.

Check this link to know "How to enable native logs".

Note :  execute '/proc/meminfo' to know the current status of memory consumed, memory free etc..

Log Function for Debugging Native Code in Android

Follow these steps to enable logs in the native code.

1) Include the "log.h" in the native code . Preferably in the beginning of the file.

          #include "android/log.h"       

2) Write the "my_log" function in the source file

void my_log(int level, const char *format,...)
    va_list arglist;

   va_start(arglist, format);
     __android_log_vprint(level, "file_name.cpp", format, arglist);


3) call "my_log()" from your function.
   int my_function()
       my_log(ANDROID_LOG_DEBUG,(const char *)"my_function has been called") ;

Note :
a) "android/log.h" doesn't define LOGE, LOGD, etc.. but only a few functions
like __android_log_print() that you are free to wrap around your own
logging/debugging macros. 

b) LOGD/LOGE are used internally by the Android sources (in <cutils/log.h>),
but are not exposed by the native code for fear of conflict with other logging