android memory leak in notification service
Asked Answered
D

2

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.

Datestamp answered 2/8, 2010 at 17:38 Comment(9)
Please post some of the Logcat right before the rebootRutabaga
It's just GC getting bigger and bigger until it causes problems and starts killing stuff and rebootsDatestamp
Make sure that it is for sure your application. Try fully uninstalling it and see if it still happens. Might be some other app you installed recently.Gestate
It is for my app, occurs in emulators too.Datestamp
Are you sure it's this service? As in if you comment out everything the service does does it still have the same problem?Ventricose
I have posted a very similar case with code and logcat output here: > https://mcmap.net/q/901421/-huge-memory-usage-in-notifications/435855 I hope it helpsHooghly
Do you see OutOfMemoryError anywhere? Either as a pop-up or in the Logcat?Earshot
I've seen the same behaviour myself - the solution was to remove the progress bar.Wastrel
Same here, but phone isn't rebooting.. Only the notification gets frozen after some time.Ingesta
S
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.

Scarce answered 19/12, 2010 at 19:30 Comment(5)
Hmmm weird, I'll try this after the holidays to confirm.Datestamp
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?Escargot
@berserk, Yes, you should.Pisano
@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?Antipyrine
@HRaval , check out my answer here: https://mcmap.net/q/901422/-android-download-file-status-bar-notification-slowing-down-phone. Basically you only want to update the notification if and only if the progress actually changed.Consort
C
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.

Correction answered 9/5, 2013 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.