1

I have an application which has (mostly) images in its layouts. As I understand, Android is supposed to free the memory when the activity is not needed anymore. Thus, I am not releasing this Drawables used by my app's layouts when the Activity goes onPause() manually. I understand Android should do this.

Then, I use this code to check the memory status:

{
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    android.app.ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(memoryInfo);

    Log.i("MEMCHECK", " memoryInfo.availMem " + memoryInfo.availMem + "\n" );
    Log.i("MEMCHECK", " memoryInfo.lowMemory " + memoryInfo.lowMemory + "\n" );
    Log.i("MEMCHECK", " memoryInfo.threshold " + memoryInfo.threshold + "\n" );

    android.os.Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(pids);
    for(android.os.Debug.MemoryInfo pidMemoryInfo: memoryInfoArray) {
        Log.i("MEMCHECK", " pidMemoryInfo.getTotalPrivateDirty(): " + pidMemoryInfo.getTotalPrivateDirty() + "\n");
        Log.i("MEMCHECK", " pidMemoryInfo.getTotalPss(): " + pidMemoryInfo.getTotalPss() + "\n");
        Log.i("MEMCHECK", " pidMemoryInfo.getTotalSharedDirty(): " + pidMemoryInfo.getTotalSharedDirty() + "\n");
}

On every created activity, the memory footprint grows (the heap - getTotalPrivateDirty() ). I don't wanna do this since every 'kb' of memory in my app is really important.

Also, when this callback is processed:

Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {

    }
}

Empty, just like that. The heap grows from 10MB to 20MB. 100%. Is this normal? How to avoid?

  • I don't think your approach for checking is valid. The problem is that you do not know exactly when Android will garbage collect. Your paused/destroyed activity may not be gc'ed until there is a need for memory, or it may be gc'ed right away. There is no (easy) way of knowing this. – THelper Jan 11 '12 at 09:44

4 Answers4

1

For images and Callbacks you need to free memory manually, instead being dependent on garbage collector. Also you need to take care of context of the views. whereas possible assign context at lowest level, like if a view is to be used only in an activity, assign it activity context, if a listener/callback should be used only in view, assign view context to it.

And also you can unbind xml drawables manually.

jeet
  • 29,001
  • 6
  • 52
  • 53
  • could you specify how you free memory of an image? Last time I checked calling `recycle` on an bitmap doesn't free memory immediately... – WarrenFaith Jan 11 '12 at 09:45
  • @jitendra sharma: can you explain more this: "like if a view is to be used only in an activity, assign it activity context, if a listener/callback should be used only in view, assign view context to it." please thanks. –  Jan 11 '12 at 09:46
  • [This post](http://stackoverflow.com/a/6779164/741249) has a code example. Also a further explanation is given [here](http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html) – THelper Jan 11 '12 at 10:09
  • i believe activity's context and a view's context used with in that activity both are same :) – waqaslam Jan 11 '12 at 10:15
  • take an example for dialog, having a button with init. If we click on any button on that dialog what would be context of button. – jeet Jan 11 '12 at 10:17
0

Android will kill the activity that is not used if other apps need memory, both in onPause and onStop. You could override the onPause or onStop to manually free up your memory.

Davos555
  • 1,974
  • 15
  • 23
  • 1
    Partly correct. Even if you app is the only running app on the device, you will not get the complete available memory. Each VM instance has just a part of the memory available... – WarrenFaith Jan 11 '12 at 09:46
0

i suppose an activity's memory is cleaned automatically once its destroyd, call finish() and check your memory in a new activity (simple one) to confirm

waqaslam
  • 67,549
  • 16
  • 165
  • 178
-1

try to use as often as possible static and final variables, this saves a lot of memory ;)

Andreas
  • 1,617
  • 15
  • 18
  • Actually using statics can be a cause of memory leaks, see [this post](http://stackoverflow.com/q/5504071/741249) – THelper Jan 11 '12 at 09:58