# 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)

*** ** * ** ***

Android/JVM
- android

  ```
  @ExperimentalRingBufferApi
  class InMemoryRingBufferTraceSink : AbstractTraceSink
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.tracing.AbstractTraceSink](https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink) ||
|   | ↳ | [androidx.tracing.wire.InMemoryRingBufferTraceSink](https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink) |

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink` that stores `https://developer.android.com/reference/kotlin/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/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#flush()` will do nothing. A `https://developer.android.com/reference/kotlin/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/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)` or `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#closeAndFlushTo(okio.BufferedSink)`.

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

## Summary

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

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#close()()` Marks the `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink` as closed and clears any remaining buffered events without writing them out. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#closeAndFlushTo(okio.BufferedSink)(bufferedSink: https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html)` Marks the `https://developer.android.com/reference/kotlin/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. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#enqueue(androidx.tracing.PooledTracePacketArray)(pooledPacketArray: https://developer.android.com/reference/kotlin/androidx/tracing/PooledTracePacketArray)` Enqueue a `https://developer.android.com/reference/kotlin/androidx/tracing/PooledTracePacketArray` to be written to the trace. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/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/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)`. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#flushTo(okio.BufferedSink)(bufferedSink: https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html)` Flushes the current buffer to the provided `https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html`. | android |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/tracing/wire/InMemoryRingBufferTraceSink#onDroppedTraceEvent()()` Called when the `https://developer.android.com/reference/kotlin/androidx/tracing/AbstractTraceSink` cannot keep up with incoming trace events from `https://developer.android.com/reference/kotlin/androidx/tracing/Track`s. | android |

## Public constructors

### InMemoryRingBufferTraceSink

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

```
InMemoryRingBufferTraceSink(
    sequenceId: @IntRange(from = 1) Int,
    capacityInBytes: Long
)
```

## Public functions

### close

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

```
open fun close(): Unit
```

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

### closeAndFlushTo

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

```
fun closeAndFlushTo(bufferedSink: BufferedSink): Unit
```

Marks the `https://developer.android.com/reference/kotlin/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

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

```
open fun enqueue(pooledPacketArray: PooledTracePacketArray): Unit
```

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

This function may be called from any thread.

### flush

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

```
open fun flush(): Unit
```

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

### flushTo

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

```
fun flushTo(bufferedSink: BufferedSink): Unit
```

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

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

```
open fun onDroppedTraceEvent(): Unit
```

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

This function may be called from any thread.