# MemoryCountersMetric

Artifact: [androidx.benchmark:benchmark-macro](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/macro/Metric.kt+class:androidx.benchmark.macro.MemoryCountersMetric) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.2.0)

*** ** * ** ***


```
@ExperimentalMetricApi
public final class MemoryCountersMetric extends TraceMetric
```

<br />

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||||
| ↳ | [androidx.benchmark.macro.Metric](https://developer.android.com/reference/androidx/benchmark/macro/Metric) |||
|   | ↳ | [androidx.benchmark.macro.TraceMetric](https://developer.android.com/reference/androidx/benchmark/macro/TraceMetric) ||
|   |   | ↳ | [androidx.benchmark.macro.MemoryCountersMetric](https://developer.android.com/reference/androidx/benchmark/macro/MemoryCountersMetric) |

*** ** * ** ***

Captures the number of page faults over time for a target package name.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/benchmark/macro/MemoryCountersMetric#MemoryCountersMetric(kotlin.String,kotlin.String)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html processNameSuffix, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html metricNameSuffix )` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/macro/Metric.Measurement>` | `https://developer.android.com/reference/androidx/benchmark/macro/MemoryCountersMetric#getMeasurements(androidx.benchmark.macro.Metric.CaptureInfo,androidx.benchmark.traceprocessor.TraceProcessor.Session)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/macro/Metric.CaptureInfo captureInfo, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/benchmark/traceprocessor/TraceProcessor.Session traceSession )` Get the metric result for a given iteration given information about the target process and a TraceProcessor session |

## Public constructors

### MemoryCountersMetric

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

```
public MemoryCountersMetric(
    @NonNull String processNameSuffix,
    @NonNull String metricNameSuffix
)
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html processNameSuffix` | A suffix appended to the app's package name for subprocesses. This is useful when there are separate subprocesses of the app. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html metricNameSuffix` | A suffix appended to the metric names. Use this to distinguish metrics collected from different subprocesses in the app. Defaults to `https://developer.android.com/reference/androidx/benchmark/macro/MemoryCountersMetric#processNameSuffix()` with ":" replaced by "_". |

## Public methods

### getMeasurements

```
public @NonNull List<@NonNull Metric.Measurement> getMeasurements(
    @NonNull Metric.CaptureInfo captureInfo,
    @NonNull TraceProcessor.Session traceSession
)
```

Get the metric result for a given iteration given information about the target process and a TraceProcessor session

```kotlin
import androidx.benchmark.macro.ExperimentalMetricApi
import androidx.benchmark.macro.TraceMetric
import androidx.benchmark.traceprocessor.TraceProcessor

/**
 * Calculates latency of sections where begin and end trace points are in different processes.
 *
 * @param beginPointName Name of begin tracepoint.
 * @param endPointName Name of end tracepoint.
 * @param eventName Name of the final metric that is spit out after the test run. The metric
 *   name will be {$eventName}LatencyMillis.
 */
@OptIn(ExperimentalMetricApi::class)
class CrossProcessLatencyMetricSample(
    private val beginPointName: String,
    private val endPointName: String,
    private val eventName: String,
) : TraceMetric() {
    @OptIn(ExperimentalMetricApi::class)
    override fun getMeasurements(
        captureInfo: CaptureInfo,
        traceSession: TraceProcessor.Session,
    ): List<Measurement> {
        val query =
            """
         INCLUDE perfetto module slices.with_context;
         SELECT
             event,
             ts2-ts1 AS latency_in_nanos
         FROM
         (
           (SELECT
               ts AS ts1,
               "event" AS event,
               LOWER(TRIM(process_name)) as process_name_1
               FROM thread_slice WHERE name = '${beginPointName}'
           )
           LEFT JOIN
           (SELECT
               ts AS ts2,
               "event" AS event,
               LOWER(TRIM(process_name)) as process_name_2
               FROM thread_slice WHERE name = '${endPointName}'
           )
           USING (event)
         )
           """
                .trimIndent() // maybe add checks for process name etc too in query

        val rowSequence = traceSession.query(query)
        // First row (or null) is returned.
        val latencyResultNanos = rowSequence.firstOrNull()?.long("latency_in_nanos")
        return if (latencyResultNanos != null) {
            listOf(Measurement(eventName + "LatencyMillis", latencyResultNanos / 1_000_000.0))
        } else {
            emptyList()
        }
    }
}
```