How to Get RAM Memory in Android Programmatically?
RAM (Random Access Memory) of a device is a system that is used to store data or information for immediate use by any application that runs on the device. Every electronic device that runs a program as a part of its application has some amount of RAM associated with it. Mobile devices nowadays come with a RAM of 4 to 16 GB depending on the model. The more the RAM, the bigger applications the phone can run. More RAM also helps in the faster and better performance of the device but is solely not responsible for better performance. So, it is important for an ideal application to keep a track of the available RAM and minimize or maximize the application processes according to it.
Through this article, we will show you how you could fetch information related to the device’s RAM programmatically in Android.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Create a TextView to display the RAM-related info.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < TextView android:id = "@+id/text_view" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:gravity = "center" android:textSize = "20sp" /> </ RelativeLayout > |
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.app.ActivityManager import android.os.Bundle import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declare and initialize TextView from the layout file val mTextView = findViewById<TextView>(R.id.text_view) // Declaring and Initializing the ActivityManager val actManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager // Declaring MemoryInfo object val memInfo = ActivityManager.MemoryInfo() // Fetching the data from the ActivityManager actManager.getMemoryInfo(memInfo) // Fetching the available and total memory and converting into Giga Bytes val availMemory = memInfo.availMem.toDouble()/( 1024 * 1024 * 1024 ) val totalMemory= memInfo.totalMem.toDouble()/( 1024 * 1024 * 1024 ) // Displaying the memory info into the TextView mTextView.text = "Available RAM: $availMemory\nTotal RAM: $totalMemory" } } |
Output:
Once the application opens, you will see Available and Total RAM available on the device for usage.
The device from which the above screenshot was taken had 6GB RAM. The difference (6 – 5.44 = 0.56) GB is already locked by the Android OS and manufacturer’s admin apps that cannot be removed from the device.