# MemoryFile

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/os/MemoryFile#pubctors)\|[Methods](https://developer.android.com/reference/android/os/MemoryFile#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/os/MemoryFile#inhmethods)  

# MemoryFile

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/os/MemoryFile "View this page in Kotlin")\|Java

`
public


class
MemoryFile
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

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

<br />

*** ** * ** ***

MemoryFile is a wrapper for[SharedMemory](https://developer.android.com/reference/android/os/SharedMemory)which can optionally be set to purgeable. Applications should generally prefer to use[SharedMemory](https://developer.android.com/reference/android/os/SharedMemory)which offers more flexible access \& control over the shared memory region than MemoryFile does. Purgeable files may have their contents reclaimed by the kernel in low memory conditions (only if allowPurging is set to true). After a file is purged, attempts to read or write the file will cause an IOException to be thrown.

## Summary

|                                                                                                            ### Public constructors                                                                                                             ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[MemoryFile](https://developer.android.com/reference/android/os/MemoryFile#MemoryFile(java.lang.String,%20int))`(`[String](https://developer.android.com/reference/java/lang/String)` name, int length) ` Allocates a new ashmem region. |

|                                                                                                                                                                                                                                                                                     ### Public methods                                                                                                                                                                                                                                                                                     ||
|---------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` boolean`                                                                      | ` `[allowPurging](https://developer.android.com/reference/android/os/MemoryFile#allowPurging(boolean))`(boolean allowPurging) ` *This method was deprecated in API level 27. Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use[ComponentCallbacks2.onTrimMemory(int)](https://developer.android.com/reference/android/content/ComponentCallbacks2#onTrimMemory(int))to react to memory events and release shared memory regions as appropriate.* |
| ` void`                                                                         | ` `[close](https://developer.android.com/reference/android/os/MemoryFile#close())`() ` Closes the memory file.                                                                                                                                                                                                                                                                                                                                                                                            |
| ` `[InputStream](https://developer.android.com/reference/java/io/InputStream)   | ` `[getInputStream](https://developer.android.com/reference/android/os/MemoryFile#getInputStream())`() ` Creates a new InputStream for reading from the memory file.                                                                                                                                                                                                                                                                                                                                      |
| ` `[OutputStream](https://developer.android.com/reference/java/io/OutputStream) | ` `[getOutputStream](https://developer.android.com/reference/android/os/MemoryFile#getOutputStream())`() ` Creates a new OutputStream for writing to the memory file.                                                                                                                                                                                                                                                                                                                                     |
| ` boolean`                                                                      | ` `[isPurgingAllowed](https://developer.android.com/reference/android/os/MemoryFile#isPurgingAllowed())`() ` *This method was deprecated in API level 27. Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use[ComponentCallbacks2.onTrimMemory(int)](https://developer.android.com/reference/android/content/ComponentCallbacks2#onTrimMemory(int))to react to memory events and release shared memory regions as appropriate.*                    |
| ` int`                                                                          | ` `[length](https://developer.android.com/reference/android/os/MemoryFile#length())`() ` Returns the length of the memory file.                                                                                                                                                                                                                                                                                                                                                                           |
| ` int`                                                                          | ` `[readBytes](https://developer.android.com/reference/android/os/MemoryFile#readBytes(byte[],%20int,%20int,%20int))`(byte[] buffer, int srcOffset, int destOffset, int count) ` Reads bytes from the memory file.                                                                                                                                                                                                                                                                                        |
| ` void`                                                                         | ` `[writeBytes](https://developer.android.com/reference/android/os/MemoryFile#writeBytes(byte[],%20int,%20int,%20int))`(byte[] buffer, int srcOffset, int destOffset, int count) ` Write bytes to the memory file.                                                                                                                                                                                                                                                                                        |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](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 `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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*.                                                                                             | ||

## Public constructors

### MemoryFile

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

```
public MemoryFile (String name, 
                int length)
```

Allocates a new ashmem region. The region is initially not purgable.

<br />

|                               Parameters                               ||
|----------|--------------------------------------------------------------|
| `name`   | `String`: optional name for the file (can be null). <br />   |
| `length` | `int`: of the memory file in bytes, must be positive. <br /> |

|                                                        Throws                                                        ||
|----------------------------------------------------------------------------|------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if the memory file could not be created. |

## Public methods

### allowPurging

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

```
public boolean allowPurging (boolean allowPurging)
```

<br />

**This method was deprecated in API level 27.**   
Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use[ComponentCallbacks2.onTrimMemory(int)](https://developer.android.com/reference/android/content/ComponentCallbacks2#onTrimMemory(int))to react to memory events and release shared memory regions as appropriate.

Enables or disables purging of the memory file.

<br />

|                                                         Parameters                                                         ||
|----------------|------------------------------------------------------------------------------------------------------------|
| `allowPurging` | `boolean`: true if the operating system can purge the contents of the file in low memory situations <br /> |

|                     Returns                      ||
|-----------|---------------------------------------|
| `boolean` | previous value of allowPurging <br /> |

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

### close

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

```
public void close ()
```

Closes the memory file. If there are no other open references to the memory file, it will be deleted.

<br />

### getInputStream

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

```
public InputStream getInputStream ()
```

Creates a new InputStream for reading from the memory file.

<br />

|                                            Returns                                             ||
|----------------------------------------------------------------------------|--------------------|
| [InputStream](https://developer.android.com/reference/java/io/InputStream) | InputStream <br /> |

### getOutputStream

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

```
public OutputStream getOutputStream ()
```

Creates a new OutputStream for writing to the memory file.

<br />

|                                              Returns                                              ||
|------------------------------------------------------------------------------|---------------------|
| [OutputStream](https://developer.android.com/reference/java/io/OutputStream) | OutputStream <br /> |

### isPurgingAllowed

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

```
public boolean isPurgingAllowed ()
```

<br />

**This method was deprecated in API level 27.**   
Purgable is considered generally fragile and hard to use safely. Applications are recommend to instead use[ComponentCallbacks2.onTrimMemory(int)](https://developer.android.com/reference/android/content/ComponentCallbacks2#onTrimMemory(int))to react to memory events and release shared memory regions as appropriate.

Is memory file purging enabled?

<br />

|                      Returns                      ||
|-----------|----------------------------------------|
| `boolean` | true if the file may be purged. <br /> |

### length

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

```
public int length ()
```

Returns the length of the memory file.

<br />

|          Returns           ||
|-------|---------------------|
| `int` | file length. <br /> |

### readBytes

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

```
public int readBytes (byte[] buffer, 
                int srcOffset, 
                int destOffset, 
                int count)
```

Reads bytes from the memory file. Will throw an IOException if the file has been purged.

<br />

|                                 Parameters                                  ||
|--------------|---------------------------------------------------------------|
| `buffer`     | `byte`: byte array to read bytes into. <br />                 |
| `srcOffset`  | `int`: offset into the memory file to read from. <br />       |
| `destOffset` | `int`: offset into the byte array buffer to read into. <br /> |
| `count`      | `int`: number of bytes to read. <br />                        |

|               Returns               ||
|-------|------------------------------|
| `int` | number of bytes read. <br /> |

|                                                             Throws                                                             ||
|----------------------------------------------------------------------------|----------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if the memory file has been purged or deactivated. |

### writeBytes

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

```
public void writeBytes (byte[] buffer, 
                int srcOffset, 
                int destOffset, 
                int count)
```

Write bytes to the memory file. Will throw an IOException if the file has been purged.

<br />

|                                  Parameters                                  ||
|--------------|----------------------------------------------------------------|
| `buffer`     | `byte`: byte array to write bytes from. <br />                 |
| `srcOffset`  | `int`: offset into the byte array buffer to write from. <br /> |
| `destOffset` | `int`: offset into the memory file to write to. <br />         |
| `count`      | `int`: number of bytes to write. <br />                        |

|                                                             Throws                                                             ||
|----------------------------------------------------------------------------|----------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if the memory file has been purged or deactivated. |