Memory Management in Android

Overview

Use this code to check if device has low memory.
 override fun onStart()
{
super.onStart()
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val memoryInformation = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInformation)
val totalMemory = memoryInformation.availMem
val megaByte = totalMemory / (1024L * 1024L).toInt()
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M && (megaByte < 300))
{
Toast.makeText(this , "Insufficient Memory to run the application" , Toast.LENGTH_LONG)
.show()
} else
{
Log.e("Memory size" , "checkLowMemory: $megaByte")
}
}

Reference



Comments

Popular posts from this blog

Architecture Components in Android

DataBinding in Android

SSLSocketFactory in Android