The random access memory, RAM, is a type of computer memory that can be accessed randomly. That is, any byte of memory can be accessed without touching the preceding bytes. It is the most common type of memory found in computers, smartphones, and embedded devices.

Digi adds to Android an API to obtain different memory values of the ConnectCore 8X modules. You can get the total, free, cached and available memory values. In the Digi APIx javadoc you can find a complete list of the available methods in this API.

Unless noted, all memory API methods require the com.digi.android.permission.MEMORY permission.

If your application does not have the com.digi.android.permission.MEMORY permission it will not have access to any memory service feature.

In order to obtain the memory values, you need to instantiate a MemoryManager object. To do so, you have to provide the application Context.

Instantiating the MemoryManager
import android.app.Activity;
import android.os.Bundle;

import com.digi.android.system.memory.MemoryManager;

public class MemorySampleActivity extends Activity {

    MemoryManager memoryManager;

    [...]

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceStace);

        // Instantiate the Memory manager object.
        memoryManager = new MemoryManager(this);

        [...]
    }

    [...]
}

Obtain the memory values

You can read the total, free, cached and available memory values using the following methods:

Method Description

getTotalMemory()

Returns the device’s total usable memory in kB (i.e. physical RAM minus a few reserved bits and the kernel binary code)

getFreeMemory()

Returns the device’s free memory in kB

getCachedMemory()

Returns the device’s in-memory cache for files read from the disk in kB

getAvailableMemory()

Returns the device’s available memory in kB. An estimate of how much memory is available for starting new applications, without swapping.

These methods may fail if there is any error while reading the memory values throwing an IOException.

Getting the memory values
import com.digi.android.system.memory.MemoryManager;

[...]

MemoryManager memoryManager = ...;

[...]

System.out.println("Total memory: " + memoryManager.getTotalMemory() + "kB");
System.out.println("Free memory: " + memoryManager.getFreeMemory() + "kB");
System.out.println("Cached memory: " + memoryManager.getCachedMemory() + "kB");
System.out.println("Available memory: " + memoryManager.getAvailableMemory() + "kB");

[...]

Memory example

Example: Memory

This API does not have a separate example. Instead, it is integrated in the CPU Management Sample Application, which demonstrates the usage of the CPU and memory APIs.

You can import the example using Digi’s Android Studio plugin. For more information, see Import a Digi sample application. To look at the application source code, go to the GitHub repository.