Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Constants](https://developer.android.com/reference/android/os/SharedMemory#inhconstants) \| [Fields](https://developer.android.com/reference/android/os/SharedMemory#lfields) \| [Methods](https://developer.android.com/reference/android/os/SharedMemory#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/SharedMemory#inhmethods)

# SharedMemory

*** ** * ** ***

` public final class SharedMemory `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/io/Closeable, https://developer.android.com/reference/android/os/Parcelable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.os.SharedMemory |

<br />

*** ** * ** ***

SharedMemory enables the creation, mapping, and protection control over anonymous shared memory.

## Summary

### Inherited constants

From interface `https://developer.android.com/reference/android/os/Parcelable` `int` `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR`

Descriptor bit used with `https://developer.android.com/reference/android/os/Parcelable#describeContents()`: indicates that the Parcelable object's flattened representation includes a file descriptor.
`int` `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`

Flag for use with `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`: the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`".

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/android/os/Parcelable.Creator<https://developer.android.com/reference/android/os/SharedMemory>` | `https://developer.android.com/reference/android/os/SharedMemory#CREATOR` <br /> |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/SharedMemory#close()() ` Close the backing `https://developer.android.com/reference/java/io/FileDescriptor` of this SharedMemory instance. |
| ` static https://developer.android.com/reference/android/os/SharedMemory` | ` https://developer.android.com/reference/android/os/SharedMemory#create(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String name, int size) ` Creates an anonymous SharedMemory instance with the provided debug name and size. |
| ` int` | ` https://developer.android.com/reference/android/os/SharedMemory#describeContents()() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| ` static https://developer.android.com/reference/android/os/SharedMemory` | ` https://developer.android.com/reference/android/os/SharedMemory#fromFileDescriptor(android.os.ParcelFileDescriptor)(https://developer.android.com/reference/android/os/ParcelFileDescriptor fd) ` Creates an instance from existing shared memory passed as `https://developer.android.com/reference/android/os/ParcelFileDescriptor`. |
| ` int` | ` https://developer.android.com/reference/android/os/SharedMemory#getSize()() ` |
| ` https://developer.android.com/reference/java/nio/ByteBuffer` | ` https://developer.android.com/reference/android/os/SharedMemory#map(int,%20int,%20int)(int prot, int offset, int length) ` Creates an mmap of the SharedMemory with the specified prot, offset, and length. |
| ` https://developer.android.com/reference/java/nio/ByteBuffer` | ` https://developer.android.com/reference/android/os/SharedMemory#mapReadOnly()() ` Creates a read-only mapping of the entire shared memory region. |
| ` https://developer.android.com/reference/java/nio/ByteBuffer` | ` https://developer.android.com/reference/android/os/SharedMemory#mapReadWrite()() ` Creates a read/write mapping of the entire shared memory region. |
| ` boolean` | ` https://developer.android.com/reference/android/os/SharedMemory#setProtect(int)(int prot) ` Sets the protection on the shared memory to the combination specified in prot, which is either a bitwise-or'd combination of `https://developer.android.com/reference/android/system/OsConstants#PROT_READ`, `https://developer.android.com/reference/android/system/OsConstants#PROT_WRITE`, `https://developer.android.com/reference/android/system/OsConstants#PROT_EXEC` from `https://developer.android.com/reference/android/system/OsConstants`, or `https://developer.android.com/reference/android/system/OsConstants#PROT_NONE`, to remove all further access. |
| ` static void` | ` https://developer.android.com/reference/android/os/SharedMemory#unmap(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer buffer) ` Unmaps a buffer previously returned by `https://developer.android.com/reference/android/os/SharedMemory#map(int,%20int,%20int)`. |
| ` void` | ` https://developer.android.com/reference/android/os/SharedMemory#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) ` Flatten this object in to a Parcel. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/android/os/Parcelable ` ` abstract int` ` https://developer.android.com/reference/android/os/Parcelable#describeContents()() `

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
` abstract void` ` https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) `

Flatten this object in to a Parcel.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Fields

### CREATOR

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final Creator<SharedMemory> CREATOR
```

<br />

<br />

## Public methods

### close

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

Close the backing `https://developer.android.com/reference/java/io/FileDescriptor` of this SharedMemory instance. Note that all open mappings of the shared memory will remain valid and may continue to be used. The shared memory will not be freed until all file descriptor handles are closed and all memory mappings are unmapped.

<br />

### create

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SharedMemory create (String name, 
                int size)
```

Creates an anonymous SharedMemory instance with the provided debug name and size. The name is only used for debugging purposes and can help identify what the shared memory is used for when inspecting memory maps for the processes that have mapped this SharedMemory instance.

<br />

| Parameters ||
|---|---|
| `name` | `String`: The debug name to use for this SharedMemory instance. This can be null, however a debug name is recommended to help identify memory usage when using tools such as lsof or examining /proc/\[pid\]/maps <br /> |
| `size` | `int`: The size of the shared memory to create. Must be greater than 0. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/SharedMemory` | A SharedMemory instance of the requested size. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/system/ErrnoException` | if the requested allocation fails. |

### describeContents

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int describeContents ()
```

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`, the return value of this method must include the `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` bit.

<br />

| Returns ||
|---|---|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either `0` or - `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` |

### fromFileDescriptor

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static SharedMemory fromFileDescriptor (ParcelFileDescriptor fd)
```

Creates an instance from existing shared memory passed as `https://developer.android.com/reference/android/os/ParcelFileDescriptor`.

The `fd` should be a shared memory created from `SharedMemory or ASharedMemory`. This can be useful when shared memory is passed as file descriptor through JNI or binder service implemented in cpp.

Note that newly created `SharedMemory` takes ownership of passed `fd` and the original `fd` becomes detached (Check `https://developer.android.com/reference/android/os/ParcelFileDescriptor#detachFd()`). If the caller wants to use the file descriptor after the call, the caller should duplicate the file descriptor (Check `https://developer.android.com/reference/android/os/ParcelFileDescriptor#dup()`) and pass the duped version instead.

<br />

| Parameters ||
|---|---|
| `fd` | `ParcelFileDescriptor`: File descriptor of shared memory passed as `https://developer.android.com/reference/android/os/ParcelFileDescriptor`. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/SharedMemory` | This value cannot be `null`. <br /> |

### getSize

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getSize ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | The size of the SharedMemory region. <br /> |

### map

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ByteBuffer map (int prot, 
                int offset, 
                int length)
```

Creates an mmap of the SharedMemory with the specified prot, offset, and length. This will always produce a new ByteBuffer window to the backing shared memory region. Every call to map() may be paired with a call to `https://developer.android.com/reference/android/os/SharedMemory#unmap(java.nio.ByteBuffer)` when the ByteBuffer returned by map() is no longer needed.

<br />

| Parameters ||
|---|---|
| `prot` | `int`: A bitwise-or'd combination of PROT_READ, PROT_WRITE, PROT_EXEC, or PROT_NONE. <br /> |
| `offset` | `int`: The offset into the shared memory to begin mapping. Must be \>= 0 and less than getSize(). <br /> |
| `length` | `int`: The length of the region to map. Must be \> 0 and offset + length must not exceed getSize(). <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer` | A ByteBuffer mapping. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/system/ErrnoException` | if the mmap call failed. |

### mapReadOnly

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ByteBuffer mapReadOnly ()
```

Creates a read-only mapping of the entire shared memory region. This requires the the protection level of the shared memory is at least PROT_READ or the map will fail. Use `https://developer.android.com/reference/android/os/SharedMemory#map(int,%20int,%20int)` to have more control over the mapping if desired. This is equivalent to map(OsConstants.PROT_READ, 0, getSize())

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer` | A ByteBuffer mapping. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/system/ErrnoException` | if the mmap call failed. |

### mapReadWrite

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ByteBuffer mapReadWrite ()
```

Creates a read/write mapping of the entire shared memory region. This requires the the protection level of the shared memory is at least PROT_READ\|PROT_WRITE or the map will fail. Use `https://developer.android.com/reference/android/os/SharedMemory#map(int,%20int,%20int)` to have more control over the mapping if desired. This is equivalent to map(OsConstants.PROT_READ \| OsConstants.PROT_WRITE, 0, getSize())

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer` | A ByteBuffer mapping. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/system/ErrnoException` | if the mmap call failed. |

### setProtect

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean setProtect (int prot)
```

Sets the protection on the shared memory to the combination specified in prot, which is either a bitwise-or'd combination of `https://developer.android.com/reference/android/system/OsConstants#PROT_READ`, `https://developer.android.com/reference/android/system/OsConstants#PROT_WRITE`, `https://developer.android.com/reference/android/system/OsConstants#PROT_EXEC` from `https://developer.android.com/reference/android/system/OsConstants`, or `https://developer.android.com/reference/android/system/OsConstants#PROT_NONE`, to remove all further access. Note that protection can only ever be removed, not added. By default shared memory is created with protection set to PROT_READ \| PROT_WRITE \| PROT_EXEC. The protection passed here also only applies to any mappings created after calling this method. Existing mmaps of the shared memory retain whatever protection they had when they were created. A common usage of this is to share a read-only copy of the data with something else. To do that first create the read/write mapping with PROT_READ \| PROT_WRITE, then call setProtect(PROT_READ) to remove write capability, then send the SharedMemory to another process. That process will only be able to mmap with PROT_READ.

<br />

| Parameters ||
|---|---|
| `prot` | `int`: Any bitwise-or'ed combination of `https://developer.android.com/reference/android/system/OsConstants#PROT_READ`, `https://developer.android.com/reference/android/system/OsConstants#PROT_WRITE`, and `https://developer.android.com/reference/android/system/OsConstants#PROT_EXEC`; or `https://developer.android.com/reference/android/system/OsConstants#PROT_NONE` <br /> |

| Returns ||
|---|---|
| `boolean` | Whether or not the requested protection was applied. Returns true on success, false if the requested protection was broader than the existing protection. <br /> |

### unmap

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void unmap (ByteBuffer buffer)
```

Unmaps a buffer previously returned by `https://developer.android.com/reference/android/os/SharedMemory#map(int,%20int,%20int)`. This will immediately release the backing memory of the ByteBuffer, invalidating all references to it. Only call this method if there are no duplicates of the ByteBuffer in use and don't access the ByteBuffer after calling this method.

<br />

| Parameters ||
|---|---|
| `buffer` | `ByteBuffer`: The buffer to unmap. This value cannot be `null`. <br /> |

### writeToParcel

Added in [API level 27](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void writeToParcel (Parcel dest, 
                int flags)
```

Flatten this object in to a Parcel.

<br />

| Parameters ||
|---|---|
| `dest` | `Parcel`: This value cannot be `null`. <br /> |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` |