# InMemoryRingBufferTraceSink

Artifact: [androidx.tracing:tracing-wire](https://developer.android.com/jetpack/androidx/releases/tracing) [View Source](https://cs.android.com/search?q=file:androidx/tracing/wire/InMemoryRingBufferTraceSink.kt+class:androidx.tracing.wire.InMemoryRingBufferTraceSink) Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

*** ** * ** ***


```
@ExperimentalRingBufferApi
public final class InMemoryRingBufferTraceSink extends AbstractTraceSink
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.tracing.AbstractTraceSink](https://developer.android.com/reference/androidx/tracing/AbstractTraceSink) ||
|   | ↳ | [androidx.tracing.wire.InMemoryRingBufferTraceSink](https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink) |

*** ** * ** ***

A `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink` that stores `https://developer.android.com/reference/androidx/tracing/TraceEvent`s in a fixed-size ring buffer to minimize allocations.

This buffer is designed to hold trace events temporarily before they are flushed. When the buffer is full, it overwrites the oldest events.

A standard `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flush()` will do nothing. A `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#close()` will simply clear the buffer and drop the events. To persist the trace data, you **must** provide a `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html` by calling `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)` or `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#closeAndFlushTo(okio.BufferedSink)`.

This implementation converts `https://developer.android.com/reference/androidx/tracing/TraceEvent`s into binary protos using [the Wire library](https://square.github.io/wire/).

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#InMemoryRingBufferTraceSink(kotlin.Int,kotlin.Long)( @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int sequenceId, long capacityInBytes )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#close()()` Marks the `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink` as closed and clears any remaining buffered events without writing them out. |
| `final void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#closeAndFlushTo(okio.BufferedSink)(@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html bufferedSink)` Marks the `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink` as closed and flushes the remaining buffered events to the provided `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html` but does not close it. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#enqueue(androidx.tracing.PooledTracePacketArray)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray pooledPacketArray)` Enqueue a `https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray` to be written to the trace. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flush()()` Intentionally a no-op because we expect the caller to explicitly flush to a sink via `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)`. |
| `final void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)(@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html bufferedSink)` Flushes the current buffer to the provided `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html`. |
| `void` | `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#onDroppedTraceEvent()()` Called when the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/androidx/tracing/Track`s. |

## Public constructors

### InMemoryRingBufferTraceSink

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public InMemoryRingBufferTraceSink(
    @IntRange(from = 1) int sequenceId,
    long capacityInBytes
)
```

## Public methods

### close

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void close()
```

Marks the `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink` as closed and clears any remaining buffered events without writing them out.

### closeAndFlushTo

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public final void closeAndFlushTo(@NonNull BufferedSink bufferedSink)
```

Marks the `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink` as closed and flushes the remaining buffered events to the provided `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html` but does not close it.

### enqueue

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void enqueue(@NonNull PooledTracePacketArray pooledPacketArray)
```

Enqueue a `https://developer.android.com/reference/androidx/tracing/PooledTracePacketArray` to be written to the trace.

This function may be called from any thread.

### flush

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void flush()
```

Intentionally a no-op because we expect the caller to explicitly flush to a sink via `https://developer.android.com/reference/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)`.

### flushTo

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public final void flushTo(@NonNull BufferedSink bufferedSink)
```

Flushes the current buffer to the provided `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html`.

This writes out the ring buffer's contents to the sink and then clears the ring buffer.

### onDroppedTraceEvent

Added in [2.0.5](https://developer.android.com/jetpack/androidx/releases/tracing#2.0.5)

```
public void onDroppedTraceEvent()
```

Called when the `https://developer.android.com/reference/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/androidx/tracing/Track`s.

This function may be called from any thread.