Open In App

How to Get RAM Memory in Android Programmatically?

Last Updated : 03 Sep, 2021
Like Article

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 version="1.0" encoding="utf-8"?>

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.


import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declare and initialize TextView from the layout file
        val mTextView = findViewById<TextView>(
        // 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
        // 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"


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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads