Huge memory usage in notifications

前端 未结 4 751
迷失自我
迷失自我 2020-12-28 09:44

I am developing an application with a service which show the progress of a timer in the notification area (with a progress bar and a text). I have extracted below a simpler

相关标签:
4条回答
  • 2020-12-28 10:24

    Try using DDMS to dump out the allocations - that should show you what objects are being allocated and where.

    My guess is that the progress bar is allocating some bitmaps on every call to setProgressBar (5 times per second) and that's what's churning through memory. What's not clear is why you are then running out - the GC seems to be picking it up, so something must be leaking.

    0 讨论(0)
  • 2020-12-28 10:32

    The problem with this workaround is that if it's an ongoing notification, it will "hop" on the status bar and notification pane as other ongoing notifications are updated.

    I've tried several things, including declaring the RemoteView and Notification members as volatile (because RemoteView is cross-thread), which seemed to work, but only slowed down the problem.

    What I settled on was using a choke member, and "cacheing" the RemoteView and Notification up to X times, then recreating them.

    When their members are set to null, the small leak seems to be freed.

    0 讨论(0)
  • 2020-12-28 10:34

    I had a similar problem. I had a Service that presented a Notification with a progress bar that corresponded to a file download. The app would crash with a OutOfMemoryError, around ten seconds after the user clicked the Notification bringing them to the app.

    I found that adding .setOngoing(true); to the Builder fixed this problem.

    public NotificationCompat.Builder setOngoing (boolean ongoing)

    Set whether this is an ongoing notification. Ongoing notifications differ from regular notifications in the following ways:

    • Ongoing notifications are sorted above the regular notifications in the notification panel.

    • Ongoing notifications do not have an 'X' close button, and are not affected by the "Clear all" button.

    Example:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setAutoCancel(true)
                                                             .setDefaults(Notification.DEFAULT_ALL)
                                                             .setContentTitle("Downloading").setContentText("Download in progress...)
                                                                 .setSmallIcon(android.R.drawable.stat_sys_download)
                                                                 .setSound(null)
                                                                 .setDefaults(0)
                                                                 .setOngoing(true);
    
    0 讨论(0)
  • 2020-12-28 10:47

    I stumbled upon the same problem... It seems like that if I 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 after 10-15 minutes because it's out of memory.

    0 讨论(0)
提交回复
热议问题