LoginSignup
0
0

More than 1 year has passed since last update.

Androidアプリのメモリについて

Posted at

はじめに

メモリリークのエラー発生時に、使用可能なメモリとメモリ使用量について調査したのでまとめました。

開発環境

PC:MacBook Pro
OS:macOS BigSur
Android Studio:Arctic Fox 2021.1.1

メモリ調査方法

ActivityManager.MemoryInfo()のメソッドを使用してメモリ情報を取得します。

MainActivity.kt
        // メモリ情報を取得
        val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val memoryInfo = ActivityManager.MemoryInfo()
        activityManager.getMemoryInfo(memoryInfo)

        // 利用可能なメモリーサイズ
        Log.d("メモリ情報", "availMem:" + memoryInfo.availMem)

        // 利用可能なメモリーサイズが不足の時、解放するか判断する、しきい値
        Log.d("メモリ情報", "threshold:" + memoryInfo.threshold)

        // システムがメモリ不足と判断しているか(メモリ不足の状況と見なす場合はtrue)
        Log.d("メモリ情報", "lowMemory:" + memoryInfo.lowMemory)

        // アクセスできる合計メモリ
        Log.d("メモリ情報", "totalMem:" + memoryInfo.totalMem)

上記を追加し実行すると以下のログが出力され、メモリ情報を確認できます。(端末によって数値は異なります)

 D/メモリ情報: availMem:1084989440
 D/メモリ情報: threshold:150994944
 D/メモリ情報: lowMemory:false
 D/メモリ情報: totalMem:2076913664

参考文献

Google developers(ActivityManager.MemoryInfo)
Google developers(アプリのメモリを管理する )

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0