14

I have a service which creates a notification and then updates it with certain information periodically. After about 12 mins or so the phone crashes and reboots, I believe it is caused by a memory leak in the following code to do with how I am updating the notification, could someone please check/advise me if this is the case and what I am doing wrong.

onCreate:

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

createNotification:

private void createNotification() {
  Intent contentIntent = new Intent(this,MainScreen.class);
  contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent appIntent =PendingIntent.getActivity(this,0, contentIntent, 0);

  contentView = new RemoteViews(getPackageName(), R.layout.notification);
  contentView.setImageViewResource(R.id.image, R.drawable.icon);
  contentView.setTextViewText(R.id.text, "");

  notification = new Notification();
  notification.when=System.currentTimeMillis();
  notification.contentView = contentView;
  notification.contentIntent = appIntent;
}

updateNotification:

private void updateNotification(String text){
  contentView.setTextViewText(R.id.text, text);
  mNotificationManager.notify(0, notification);
}

Thanks in advance.

Tyler
  • 21,762
  • 11
  • 61
  • 90
stealthcopter
  • 13,964
  • 13
  • 65
  • 83

2 Answers2

9

I stumbled upon the same problem. Looks like that if you don't "cache" the RemoteView and Notification in the service, but re-create them from scratch in the "update" routine this problem disappears. Yes, I know it is not efficient, but at least the phone does not reboot from out of memory errors.

haimg
  • 4,547
  • 35
  • 47
  • Hmmm weird, I'll try this after the holidays to confirm. – stealthcopter Dec 22 '10 at 21:06
  • Hi, I am updating a notification's Remote View (precisely 3 textviews inside it) continuously (after each second) using a service. The phone gets super slow and freezes after some time. Should I recreate notification for this problem too? – berserk Jan 09 '14 at 05:07
  • @berserk, Yes, you should. – grepsedawk Mar 10 '14 at 08:30
  • @Sakiboy as you have said dont update it too often...but i am using progress to show download...so i have to update it as per my download...what can i do? – H Raval Aug 05 '16 at 10:51
  • @HRaval , check out my answer here: http://stackoverflow.com/a/38402214/2371425. Basically you only want to update the notification if and only if the progress actually changed. – Sakiboy Aug 09 '16 at 00:09
2

I had the very same problem. My solution is close to the one that @haimg said, but I do cache the notification (just the RemoteView is recreated). By doing so, the notification won't flash again if you are looking at it.

Example:

public void createNotification(Context context){
    Notification.Builder builder = new Notification.Builder(context);

    // Set notification stuff...

    // Build the notification
    notification = builder.build();
}

public void updateNotification(){
    notification.bigContentView = getBigContentView();
    notification.contentView = getCompactContentView();

    mNM.notify(NOTIFICATION_ID, notification);
}

And in the methods getBigContentView and getCompactContentView I return a new RemoteViews with the updated layout.

Alesqui
  • 6,417
  • 5
  • 39
  • 43