Shared Memory

Additional

Language
Java
Version
N/A
Created
Mar 19, 2017
Updated
Jul 23, 2018
Owner
Newtron Labs (NewtronLabs)
Contributor
Newtron Labs (NewtronLabs)
1
Activity
Badge
Generate
Download
Source code
APK file

Show card

Shared Memory

The Shared Memory library allows for the creation of memory regions that may be simultaneously accessed by multiple Android processes or applications. Developed to overcome the Android 1MB IPC limitation, this Shared Memory library allows you to exchange larger amounts of data between your Android applications.


In the build.gradle for your app.

dependencies {
    compileOnly 'com.newtronlabs.sharedmemory:sharedmemory:2.0.0'
}

Sharing Memory - Producer

From the application that wishes to shared its memory, allocate a shated memory region with a given name.

// Allocate 2MB
int sizeInBytes = 2*(1024*1024);
String regionName = "Test-Region";
ISharedMemory sharedMemory = SharedMemoryProducer.getInstance().allocate(regionName, sizeInBytes);

Write data to memory:

byte[] strBytes = "Hello World!".getBytes();
sharedMemory.writeBytes(strBytes, 0, 0, strBytes.length);

Once an application has shared a memory region it can be accessed by other processes or application which are aware of it.

Accessing Shared Memory - Consumer

In order for an application to access a region of memory shaered by an external application perform the following:

// This is the application id of the application or process which shared the region.
String producerAppId = "com.newtronlabs.smproducerdemo";

// Name under wich the remote region was created.
String regionName = "Test-Region"

// Note: The remote application must have allocated a memory region with the same
//       name or this call will fail and return null.
IRemoteSharedMemory remoteMemory 
         = RemoteMemoryAdapter.getDefaultAdapter().getSharedMemory(context, producerAppId, regionName);

// Allocate memory to read shared content.
byte[] dataBytes = new byte[remoteMemory.getSize()];
String dataStr = new String(dataBytes);
Log.d("Newtron", "Memory Read:"+dataStr);

Additional Samples

A set of more complex exmaples can be found in this repo's samples folders: SmProducer and SmConsumer.