# DefaultAllocator

Artifact: [androidx.media3:media3-exoplayer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/exoplayer/upstream/DefaultAllocator.java+class:androidx.media3.exoplayer.upstream.DefaultAllocator)

*** ** * ** ***


```
@UnstableApi
public final class DefaultAllocator implements Allocator
```

<br />

*** ** * ** ***

Default implementation of `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#DefaultAllocator(boolean,int)(boolean trimOnReset, int individualAllocationSize)` Constructs an instance without creating any `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`s up front. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#DefaultAllocator(boolean,int,int)( boolean trimOnReset, int individualAllocationSize, int initialAllocationCount )` Constructs an instance with some `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`s created up front. |

| ### Public methods |
|---|---|
| `synchronized https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#allocate()()` Obtain an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |
| `int` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#getIndividualAllocationLength()()` Returns the length of each individual `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |
| `synchronized int` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#getTotalBytesAllocated()()` Returns the total number of bytes currently allocated. |
| `synchronized int` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#getUnusedBytesAllocated()()` Returns the total number of bytes currently occupied by unused allocations. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#release(androidx.media3.exoplayer.upstream.Allocation)(https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation allocation)` Releases an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` back to the allocator. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#release(androidx.media3.exoplayer.upstream.Allocator.AllocationNode)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator.AllocationNode allocationNode)` Releases all `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` in the chain starting at the given `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator.AllocationNode`. |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#reset()()` |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#setTargetBufferSize(int)(int targetBufferSize)` |
| `synchronized void` | `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#trim()()` Hints to the allocator that it should make a best effort to release any excess `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |

## Public constructors

### DefaultAllocator

```
public DefaultAllocator(boolean trimOnReset, int individualAllocationSize)
```

Constructs an instance without creating any `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`s up front.

| Parameters |
|---|---|
| `boolean trimOnReset` | Whether memory is freed when the allocator is reset. Should be true unless the allocator will be re-used by multiple player instances. If set to false, trimming can be forced by calling `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#setTargetBufferSize(int)` manually when required. |
| `int individualAllocationSize` | The length of each individual `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |

### DefaultAllocator

```
public DefaultAllocator(
    boolean trimOnReset,
    int individualAllocationSize,
    int initialAllocationCount
)
```

Constructs an instance with some `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`s created up front.

Note: `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`s created up front will never be discarded by `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#trim()`.

| Parameters |
|---|---|
| `boolean trimOnReset` | Whether memory is freed when the allocator is reset. Should be true unless the allocator will be re-used by multiple player instances. If set to false, trimming can be forced by calling `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/DefaultAllocator#setTargetBufferSize(int)` manually when required. |
| `int individualAllocationSize` | The length of each individual `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |
| `int initialAllocationCount` | The number of allocations to create up front. |

## Public methods

### allocate

```
synchronized public Allocation allocate()
```

Obtain an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`.

When the caller has finished with the `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`, it should be returned by calling `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator#release(androidx.media3.exoplayer.upstream.Allocation)`.

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` | The `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`. |

### getIndividualAllocationLength

```
public int getIndividualAllocationLength()
```

Returns the length of each individual `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`.

### getTotalBytesAllocated

```
synchronized public int getTotalBytesAllocated()
```

Returns the total number of bytes currently allocated.

### getUnusedBytesAllocated

```
synchronized public int getUnusedBytesAllocated()
```

Returns the total number of bytes currently occupied by unused allocations.

### release

```
synchronized public void release(Allocation allocation)
```

Releases an `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` back to the allocator.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation allocation` | The `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` being released. |

### release

```
synchronized public void release(@Nullable Allocator.AllocationNode allocationNode)
```

Releases all `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation` in the chain starting at the given `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocator.AllocationNode`.

Implementations must not make memory allocations.

### reset

```
synchronized public void reset()
```

### setTargetBufferSize

```
synchronized public void setTargetBufferSize(int targetBufferSize)
```

### trim

```
synchronized public void trim()
```

Hints to the allocator that it should make a best effort to release any excess `https://developer.android.com/reference/androidx/media3/exoplayer/upstream/Allocation`.