1

I just found that using Trace.beginSection can help troubleshoot performance issues while using systrace tool. The documentation states "This tracing mechanism is independent of the method tracing mechanism offered by Debug#startMethodTracing"

But just like debug logs, do we need to comment/remove Trace API before releasing the app. Or is it allowed/benign to leave Trace API invocations in release code?

Preparing for release checklist does not mention anything about trace API.

But I am doubtful because there are other methods like Trace.setCounter which can be used to log debug info.

patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
Amit
  • 173
  • 7

2 Answers2

1

Typically I remove the traces when I'm done with them but Google leaves it in for some of the most basic building blocks like RecyclerView so it's probably fine.

Here's an example from the RecyclerView source:

// androidx.recyclerview.widget.RecyclerView

void consumePendingUpdateOperations() {
    if (!mFirstLayoutComplete || mDataSetHasChangedAfterLayout) {
        TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG);
        dispatchLayout();
        TraceCompat.endSection();
        return;
    }
    if (!mAdapterHelper.hasPendingUpdates()) {
        return;
    }
                                                                                            
    // if it is only an item change (no add-remove-notifyDataSetChanged) we can check if any
    // of the visible items is affected and if not, just ignore the change.
    if (mAdapterHelper.hasAnyUpdateTypes(AdapterHelper.UpdateOp.UPDATE) && !mAdapterHelper
            .hasAnyUpdateTypes(AdapterHelper.UpdateOp.ADD | AdapterHelper.UpdateOp.REMOVE
                    | AdapterHelper.UpdateOp.MOVE)) {
        TraceCompat.beginSection(TRACE_HANDLE_ADAPTER_UPDATES_TAG);
        startInterceptRequestLayout();
        onEnterLayoutOrScroll();
        mAdapterHelper.preProcess();
        if (!mLayoutWasDefered) {
            if (hasUpdatedView()) {
                dispatchLayout();
            } else {
                // no need to layout, clean state
                mAdapterHelper.consumePostponedUpdates();
            }
        }
        stopInterceptRequestLayout(true);
        onExitLayoutOrScroll();
        TraceCompat.endSection();
    } else if (mAdapterHelper.hasPendingUpdates()) {
        TraceCompat.beginSection(TRACE_ON_DATA_SET_CHANGE_LAYOUT_TAG);
        dispatchLayout();
        TraceCompat.endSection();
    }
}
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
0

It's okay to leave them in but do not leave any confidential information because it will be exposed to anyone profiling the app.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 01:59