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

# Trace

*** ** * ** ***

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


`
public

final

class
Trace
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

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

<br />

*** ** * ** ***

Writes trace events to the system trace buffer. These trace events can be
collected and visualized using the Systrace tool.

This tracing mechanism is independent of the method tracing mechanism
offered by `https://developer.android.com/reference/android/os/Debug#startMethodTracing()`. In particular, it enables
tracing of events that occur across multiple processes.

For information about using the Systrace tool, read [Analyzing Display and Performance
with Systrace](https://developer.android.com/tools/debugging/systrace).

## Summary

| ### Public methods ||
|---|---|
| ` static void` | ` https://developer.android.com/reference/android/os/Trace#beginAsyncSection(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String methodName, int cookie) ` Writes a trace message to indicate that a given section of code has begun. |
| ` static void` | ` https://developer.android.com/reference/android/os/Trace#beginSection(java.lang.String)(https://developer.android.com/reference/java/lang/String sectionName) ` Writes a trace message to indicate that a given section of code has begun. |
| ` static void` | ` https://developer.android.com/reference/android/os/Trace#endAsyncSection(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String methodName, int cookie) ` Writes a trace message to indicate that the current method has ended. |
| ` static void` | ` https://developer.android.com/reference/android/os/Trace#endSection()() ` Writes a trace message to indicate that a given section of code has ended. |
| ` static boolean` | ` https://developer.android.com/reference/android/os/Trace#isEnabled()() ` Checks whether or not tracing is currently enabled. |
| ` static void` | ` https://developer.android.com/reference/android/os/Trace#setCounter(java.lang.String,%20long)(https://developer.android.com/reference/java/lang/String counterName, long counterValue) ` Writes trace message to indicate the value of a given counter. |

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

### beginAsyncSection

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

```
public static void beginAsyncSection (String methodName, 
                int cookie)
```

Writes a trace message to indicate that a given section of code has
begun. Must be followed by a call to `https://developer.android.com/reference/android/os/Trace#endAsyncSection(java.lang.String,%20int)` with the same
methodName and cookie. Unlike `https://developer.android.com/reference/android/os/Trace#beginSection(java.lang.String)` and `https://developer.android.com/reference/android/os/Trace#endSection()`,
asynchronous events do not need to be nested. The name and cookie used to
begin an event must be used to end it.

<br />

| Parameters ||
|---|---|
| `methodName` | `String`: The method name to appear in the trace. This value cannot be `null`. <br /> |
| `cookie` | `int`: Unique identifier for distinguishing simultaneous events <br /> |

### beginSection

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

```
public static void beginSection (String sectionName)
```

Writes a trace message to indicate that a given section of code has begun. This call must
be followed by a corresponding call to `https://developer.android.com/reference/android/os/Trace#endSection()` on the same thread.

At this time the vertical bar character '\|', newline character '\\n', and
null character '\\0' are used internally by the tracing mechanism. If sectionName contains
these characters they will be replaced with a space character in the trace.

<br />

| Parameters ||
|---|---|
| `sectionName` | `String`: The name of the code section to appear in the trace. This may be at most 127 Unicode code units long. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if `sectionName` is too long. |

### endAsyncSection

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

```
public static void endAsyncSection (String methodName, 
                int cookie)
```

Writes a trace message to indicate that the current method has ended.
Must be called exactly once for each call to `https://developer.android.com/reference/android/os/Trace#beginAsyncSection(java.lang.String,%20int)`
using the same name and cookie.

<br />

| Parameters ||
|---|---|
| `methodName` | `String`: The method name to appear in the trace. This value cannot be `null`. <br /> |
| `cookie` | `int`: Unique identifier for distinguishing simultaneous events <br /> |

### endSection

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

```
public static void endSection ()
```

Writes a trace message to indicate that a given section of code has ended. This call must
be preceeded by a corresponding call to `https://developer.android.com/reference/android/os/Trace#beginSection(java.lang.String)`. Calling this method
will mark the end of the most recently begun section of code, so care must be taken to
ensure that beginSection / endSection pairs are properly nested and called from the same
thread.

<br />

### isEnabled

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

```
public static boolean isEnabled ()
```

Checks whether or not tracing is currently enabled. This is useful to avoid intermediate
string creation for trace sections that require formatting. It is not necessary
to guard all Trace method calls as they internally already check this. However it is
recommended to use this to prevent creating any temporary objects that would then be
passed to those methods to reduce runtime cost when tracing isn't enabled.

<br />

| Returns ||
|---|---|
| `boolean` | true if tracing is currently enabled, false otherwise <br /> |

### setCounter

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

```
public static void setCounter (String counterName, 
                long counterValue)
```

Writes trace message to indicate the value of a given counter.

<br />

| Parameters ||
|---|---|
| `counterName` | `String`: The counter name to appear in the trace. This value cannot be `null`. <br /> |
| `counterValue` | `long`: The counter value. <br /> |