cordovaプラグインlocal-notificationsでのメモリ不足回避

CordovaでAndroidアプリを開発している際に、cordova-plugin-local-notificationsを利用してローカル通知を大量に登録(300件くらい?)する際に以下のエラーでアプリが毎回落ちました。

問題点としては、通知として登録する画像のサイズが大きすぎたことでメモリを大量に消費していました。

エラー内容

Androidのデベロッパーコンソールへ送られてきたエラー内容

java.lang.OutOfMemoryError: Failed to allocate a 1048588 byte allocation with 828770 free bytes and 809KB until OOM
 at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
 at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
 at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
 at de.appplant.cordova.plugin.notification.AssetUtil.getIconFromUri(AssetUtil.java:367)
 at de.appplant.cordova.plugin.notification.Options.getIconBitmap(Options.java:293)
 at de.appplant.cordova.plugin.notification.Builder.build(Builder.java:141)
 at de.appplant.cordova.plugin.notification.Manager.get(Manager.java:416)
 at de.appplant.cordova.plugin.notification.Manager.getByIds(Manager.java:234)
 at de.appplant.cordova.plugin.notification.Manager.getAll(Manager.java:248)
 at de.appplant.cordova.plugin.notification.Manager.cancelAll(Manager.java:178)
 at de.appplant.cordova.plugin.localnotification.LocalNotification.cancelAll(LocalNotification.java:268)
 at de.appplant.cordova.plugin.localnotification.LocalNotification.access$300(LocalNotification.java:50)
 at de.appplant.cordova.plugin.localnotification.LocalNotification$1.run(LocalNotification.java:150)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
 at java.lang.Thread.run(Thread.java:818)

**java.lang.OutOfMemoryError**

とりあえず、メモリが足りないらしいです。

 

解決策

参考: Android crash java.lang.OutOfMemoryError #883

今まで、通知に用いる画像をよくわからないまま512×512で作成したいましたが、どうやら144×144で良さげらしいので、変更したらメモリ不足で落ちる事もなくなりました。

 

ImageMagicのコマンドを利用した画像サイズの変更

convert icon.png -resize 144x icon_144.png
# もとの画像は正方形だとして、144x144の画像が出力される。

One thought on “cordovaプラグインlocal-notificationsでのメモリ不足回避

  1. Pingback: CordovaでAndroidアプリを作成する – その2 | eye4brain

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください