【问题标题】:Memory leak from recieving lots of FCM messages接收大量 FCM 消息导致内存泄漏
【发布时间】:2020-04-06 22:16:29
【问题描述】:

我们目前正在开发一个使用 Pusher Beams 的 kotlin 应用程序。它运行得非常好,直到每秒收到一定数量的通知(4+)。然后通常会因这两个例外之一而失败:

Fatal Exception: java.lang.OutOfMemoryError: Could not allocate JNI Env
   at java.lang.Thread.nativeCreate(Thread.java)
   at java.lang.Thread.start(Thread.java:730)
   at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941)
   at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1359)
   at okhttp3.ConnectionPool.put(ConnectionPool.java:153)

或者

Fatal Exception: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. 
   at android.database.CursorWindow.<init>(CursorWindow.java:108)
   at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
   at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:138)
   at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)

由于这两个异常似乎都拒绝分配更多内存,我们使用 Profiler 分析了应用程序。那里的消费看起来很稳定,直到它崩溃。还添加了不报告任何(已知)泄漏的 LeakCanary... 只有 MessagingService 的实例和 okhttp 的连接似乎存在/打开的时间比必要的时间长。

这是我的服务的 sn-p:

class NotificationService : MessagingService() {

    ...

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        val data = remoteMessage.data

        if(data.containsKey("action")){
            val action = data["action"].toString()

            if(action == "ticketUpdate"){
                val json = Json(JsonConfiguration.Stable)
                val tickets: List<Ticket> = json.parse(Ticket.serializer().list, data["tickets"].toString())

                val ticketIterator = tickets.iterator()
                ticketIterator.forEach { ticket ->
                    ticketHelper.updateTicket(ticket)
                }
            }
        }
    }
}
  • 我们是否错误地处理传入的消息?
  • 如何继续调试这个问题?
  • 我在throughput of fcm messages 上没有发现任何限制。推进器部分是否存在任何已知问题?

【问题讨论】:

    标签: kotlin firebase-cloud-messaging pusher


    【解决方案1】:

    花了一段时间,但解决了我的问题:

    https://github.com/pusher/push-notifications-android/pull/106

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      相关资源
      最近更新 更多