Define custom events
Stay organized with collections
Save and categorize content based on your preferences.
System tracing shows you information about processes only at the system level,
so it's sometimes difficult to know which of your app or game's methods are
executing at a given time relative to system events.
Jetpack provides a tracing API that you can use to label a particular section of
code. This information is then reported in traces captured on the device.
Macrobenchmark
captures traces with custom trace points automatically.
When using the systrace command line tool to capture traces, the -a
option is
required. Without this option, your app's methods don't appear in a system
trace report.
Kotlin
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): MyViewHolder {
trace("MyAdapter.onCreateViewHolder") {
MyViewHolder.newInstance(parent)
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
trace("MyAdapter.onBindViewHolder") {
trace("MyAdapter.queryDatabase")
val rowItem = queryDatabase(position)
dataset.add(rowItem)
}
holder.bind(dataset[position])
}
}
}
Java
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return TraceKt.trace(
"MyAdapter.onCreateViewHolder",
() -> MyViewHolder.newInstance(parent)
);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
TraceKt.trace(
"MyAdapter.onBindViewHolder",
() -> {
TraceKt.trace(
"MyAdapter.queryDatabase",
() -> {
Item rowItem = queryDatabase(position);
dataset.add(rowItem);
}
);
}
);
}
}
We recommend using the Kotlin extension function, even in Java code, as it
automatically ends the trace when the lambda completes. This removes the risk
of forgetting to end the tracing.
You can also use an NDK API for custom trace events. To learn about using this
API for your native code, see Custom trace events in native
code.
Recommended for you
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-01-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-01-03 UTC."],[],[],null,["# Define custom events\n\nSystem tracing shows you information about processes only at the system level,\nso it's sometimes difficult to know which of your app or game's methods are\nexecuting at a given time relative to system events.\n\nJetpack provides a tracing API that you can use to label a particular section of\ncode. This information is then reported in traces captured on the device.\n[Macrobenchmark](/topic/performance/benchmarking/macrobenchmark-overview)\ncaptures traces with custom trace points automatically.\n\nWhen using the systrace command line tool to capture traces, the `-a` option is\nrequired. Without this option, your app's methods don't appear in a system\ntrace report. \n\n### Kotlin\n\n```kotlin\nclass MyAdapter : RecyclerView.Adapter\u003cMyViewHolder\u003e() {\n override fun onCreateViewHolder(parent: ViewGroup,\n viewType: Int): MyViewHolder {\n trace(\"MyAdapter.onCreateViewHolder\") {\n MyViewHolder.newInstance(parent)\n }\n }\n\n override fun onBindViewHolder(holder: MyViewHolder, position: Int) {\n trace(\"MyAdapter.onBindViewHolder\") {\n trace(\"MyAdapter.queryDatabase\")\n val rowItem = queryDatabase(position)\n dataset.add(rowItem)\n }\n holder.bind(dataset[position])\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class MyAdapter extends RecyclerView.Adapter\u003cMyViewHolder\u003e {\n @NonNull\n @Override\n public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {\n return TraceKt.trace(\n \"MyAdapter.onCreateViewHolder\",\n () -\u003e MyViewHolder.newInstance(parent)\n );\n }\n\n @Override\n public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {\n TraceKt.trace(\n \"MyAdapter.onBindViewHolder\",\n () -\u003e {\n TraceKt.trace(\n \"MyAdapter.queryDatabase\",\n () -\u003e {\n Item rowItem = queryDatabase(position);\n dataset.add(rowItem);\n }\n );\n }\n );\n }\n}\n```\n\nWe recommend using the Kotlin extension function, even in Java code, as it\nautomatically ends the trace when the lambda completes. This removes the risk\nof forgetting to end the tracing.\n\nYou can also use an NDK API for custom trace events. To learn about using this\nAPI for your native code, see [Custom trace events in native\ncode](/topic/performance/tracing/custom-events-native).\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [App startup time](/topic/performance/vitals/launch-time)\n- [Slow rendering](/topic/performance/vitals/render)"]]