Android 15 (एपीआई लेवल 35) और इसके बाद के वर्शन पर, ऐप्लिकेशन के हिसाब से प्रोफ़ाइल बनाने की सुविधा की मदद से, आपका ऐप्लिकेशन कोड में प्रोफ़ाइल की रिकॉर्डिंग को साफ़ तौर पर शुरू और बंद कर सकता है. इसके लिए, ProfilingManager एपीआई या AndroidX Core (androidx.core:core 1.15.0 या इसके बाद के वर्शन) बिल्डर का इस्तेमाल किया जा सकता है. डायरेक्ट प्रोफ़ाइलिंग का इस्तेमाल तब करें, जब आपको प्रोफ़ाइलिंग के समय या सेशन के पैरामीटर को कंट्रोल करना हो. जैसे, अवधि, बफ़र नीतियां, और सैंपलिंग रेट. इसके अलावा, इसका इस्तेमाल तब करें, जब आपको उपयोगकर्ता की किसी खास गतिविधि या रनटाइम की स्थिति को टारगेट करना हो.
डिपेंडेंसी जोड़ें
ProfilingManager एपीआई का इस्तेमाल करने का सबसे अच्छा अनुभव पाने के लिए, अपनी build.gradle.kts फ़ाइल में ये Jetpack लाइब्रेरी जोड़ें.
Kotlin
dependencies { implementation("androidx.tracing:tracing-ktx:2.0.5") implementation("androidx.core:core:1.19.1") }
Groovy
dependencies { implementation 'androidx.tracing:tracing-ktx:2.0.5' implementation 'androidx.core:core:1.19.1' }
सिस्टम ट्रेस करने की प्रक्रिया को रिकॉर्ड करना
ज़रूरी डिपेंडेंसी जोड़ने के बाद, सिस्टम ट्रेस रिकॉर्ड करने के लिए इस कोड का इस्तेमाल करें. इस उदाहरण में, प्रोफ़ाइलिंग सेशन शुरू करने का तरीका बताया गया है. साथ ही, मुख्य थ्रेड से बाहर के भारी-भरकम ऑपरेशन को सुरक्षित तरीके से मैनेज करने का तरीका भी बताया गया है.
Kotlin
@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) @Composable fun ProfiledScreen(modifier: Modifier = Modifier) { // Use the application context: requestProfiling resolves the ProfilingManager // system service from it, so there's no reason to hand it a short-lived Activity. val appContext = LocalContext.current.applicationContext val scope = rememberCoroutineScope() Button( onClick = { // Run the orchestration off the main thread. Profiling a heavy operation // on the UI thread would freeze the UI (ANR) and distort the very metrics // you're trying to capture. // // Note: this scope is tied to composition. If the user leaves this screen // mid-session, the coroutine is cancelled and stopSignal.cancel() might not // run, but setDurationMs() acts as a safety net and ends the profile. scope.launch(Dispatchers.Default) { val callbackExecutor = Dispatchers.IO.asExecutor() val resultCallback = Consumer<ProfilingResult> { profilingResult -> if (profilingResult.errorCode == ProfilingResult.ERROR_NONE) { Log.d("ProfileTest", "Result file: ${profilingResult.resultFilePath}") } else { // errorMessage explains the failure (e.g., rate limiting); keep it. Log.e( "ProfileTest", "Profiling failed errorCode=${profilingResult.errorCode} " + "errorMessage=${profilingResult.errorMessage}" ) } } val stopSignal = CancellationSignal() val requestBuilder = SystemTraceRequestBuilder().apply { setCancellationSignal(stopSignal) setTag("FOO") // Caller-supplied tag for identification. setDurationMs(60000) // Hard cap: ends the session if cancel() never fires. setBufferFillPolicy(BufferFillPolicy.RING_BUFFER) setBufferSizeKb(32768) } // 1. Start the session. This is asynchronous system IPC. The tracing // engine takes a moment to start and allocate buffers. requestProfiling(appContext, requestBuilder.build(), callbackExecutor, resultCallback) // 2. The API exposes no "profiling started" signal, so pad with a short, // best-effort delay before running the code you care about. This is // approximate. Increase it on slower or heavily loaded devices. delay(STARTUP_PADDING_MS) // 3. The session is already recording every thread in your app. This slice // doesn't scope what's captured. It just labels this region of the // timeline so heavyOperation() is easier to find. trace { } closes the // section even if the block throws. trace("MyApp:HeavyOperation") { heavyOperation() } // 4. Stop recording. Until this fires or the setDurationMs() cap is // reached (whichever comes first), the session keeps capturing app-wide // activity. stopSignal.cancel() } } ) { Text("Run & Profile Heavy Operation") } } // Best-effort wait for the system trace engine to initialize before profiling. // There is no deterministic start callback; tune this for your target devices. private const val STARTUP_PADDING_MS = 100L fun heavyOperation() { // Background computations to profile. }
Java
void sampleRecordSystemTrace(Context context) { // Use the application context: requestProfiling resolves the ProfilingManager // system service from it, so there's no reason to hand it a short-lived Activity. Context appContext = context.getApplicationContext(); // Run the orchestration off the main thread. Profiling a heavy operation // on the UI thread would freeze the UI (ANR) and distort the very metrics // you're trying to capture. profilingExecutor.execute(() -> { Executor callbackExecutor = Executors.newSingleThreadExecutor(); Consumer<ProfilingResult> resultCallback = profilingResult -> { if (profilingResult.getErrorCode() == ProfilingResult.ERROR_NONE) { Log.d("ProfileTest", "Result file: " + profilingResult.getResultFilePath()); } else { // errorMessage explains the failure (e.g., rate limiting); keep it. Log.e( "ProfileTest", "Profiling failed errorCode=" + profilingResult.getErrorCode() + " errorMessage=" + profilingResult.getErrorMessage()); } }; CancellationSignal stopSignal = new CancellationSignal(); SystemTraceRequestBuilder requestBuilder = new SystemTraceRequestBuilder(); requestBuilder.setCancellationSignal(stopSignal); requestBuilder.setTag("FOO"); // Caller-supplied tag for identification. requestBuilder.setDurationMs(60000); // Hard cap: ends the session if cancel() never fires. requestBuilder.setBufferFillPolicy(BufferFillPolicy.RING_BUFFER); requestBuilder.setBufferSizeKb(32768); // 1. Start the session. This is asynchronous system IPC. The tracing // engine takes a moment to start and allocate buffers. Profiling.requestProfiling( appContext, requestBuilder.build(), callbackExecutor, resultCallback); // 2. The API exposes no "profiling started" signal, so pad with a short, // best-effort delay before running the code you care about. This is // approximate. Increase it on slower or heavily loaded devices. try { Thread.sleep(STARTUP_PADDING_MS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); stopSignal.cancel(); return; } // 3. The session is already recording every thread in your app. This slice // doesn't scope what's captured. It just labels this region of the // timeline so heavyOperation() is easier to find. try/finally closes the // section even if heavyOperation() throws. Trace.beginSection("MyApp:HeavyOperation"); try { heavyOperation(); } finally { Trace.endSection(); } // 4. Stop recording. Until this fires or the setDurationMs() cap is // reached (whichever comes first), the session keeps capturing app-wide // activity. stopSignal.cancel(); }); } // Best-effort wait for the system trace engine to initialize before profiling. // There is no deterministic start callback; tune this for your target devices. private static final long STARTUP_PADDING_MS = 100L; // Single background thread that runs the profiling orchestration. private final ExecutorService profilingExecutor = Executors.newSingleThreadExecutor(); void heavyOperation() { // Background computations to profile. }
सैंपल कोड, इन चरणों में प्रोफ़ाइलिंग सेशन को सेट अप और मैनेज करता है:
- एग्ज़ीक्यूटर सेट अप करना: मुख्य थ्रेड से प्रोफ़ाइलिंग के नतीजे पाने के लिए, नॉन-यूज़र इंटरफ़ेस
Executorबनाएं. - प्रोफ़ाइलिंग के नतीजे मैनेज करना:
ProfilingManagerसे पूरा नतीजा या गड़बड़ी का मैसेज पाने के लिए,Consumer<ProfilingResult>कॉलबैक बनाएं. प्रोफ़ाइलिंग का अनुरोध बनाएं: सेशन की सेटिंग को पसंद के मुताबिक बनाने के लिए,
SystemTraceRequestBuilderबनाएं (या सिस्टम की डिफ़ॉल्ट सेटिंग का इस्तेमाल करने के लिए, पसंद के मुताबिक बनाने की सुविधा को छोड़ें):setTag(): कॉलर से मिले टैग को अटैच करता है. यह टैग,ProfilingResult.getTagसे मिलता है और आउटपुट फ़ाइल के नाम में जोड़ा जाता है. इसमें अंग्रेज़ी के छोटे अक्षरों, अंकों, और हाइफ़न का इस्तेमाल किया जा सकता है. हालांकि, इसमें ज़्यादा से ज़्यादा 20 वर्ण इस्तेमाल किए जा सकते हैं.setDurationMs(): इससे रिकॉर्डिंग की ज़्यादा से ज़्यादा अवधि को मिलीसेकंड में सेट किया जाता है. उदाहरण के लिए, 60 सेकंड के लिए60000.setBufferFillPolicy(): इससे तय होता है कि बफ़र, क्षमता की सीमाओं को कैसे मैनेज करता है:BufferFillPolicy.RING_BUFFERबफ़र भरने पर सबसे पुराने डेटा को ओवरराइट कर देता है, जबकिBufferFillPolicy.DISCARDबफ़र भरने के बाद नया डेटा रिकॉर्ड करना बंद कर देता है.setBufferSizeKb(): रिकॉर्डिंग बफ़र साइज़ को किलोबाइट में सेट करता है.
ज़रूरी नहीं: रिकॉर्डिंग को जल्दी बंद करना: टारगेट ऑपरेशन खत्म होते ही सेशन को बंद करने के लिए,
CancellationSignal(stopSignal) पास करें. इससे अब तक रिकॉर्ड किया गया डेटा सेव हो जाता है और आपके कॉलबैक कोProfilingResultडिलीवर हो जाता है. ज़्यादा जानकारी के लिए, चल रही रिकॉर्डिंग को रोकना लेख पढ़ें.रिकॉर्डिंग शुरू करें और नतीजे पाएं: बैकग्राउंड में रिकॉर्डिंग शुरू करने के लिए,
requestProfiling()को कॉल करें. सेशन खत्म होने पर,resultCallback.accept()कोProfilingResultमिलता है:- सफलता (
ERROR_NONE) मिलने पर, डिवाइस पर मौजूद पाथ पाने के लिएgetResultFilePath()को कॉल करें.adbकी मदद से फ़ाइलें पाने याWorkManagerकी मदद से उन्हें अपलोड करने के बारे में जानने के लिए, प्रोफ़ाइलें वापस पाना और अपलोड करना लेख पढ़ें. - अगर गड़बड़ी होती है, तो
getErrorCode()औरgetErrorMessage()की जांच करें. अनुरोध संख्या सीमित करने के साथ-साथ कई अन्य वजहों से भी अनुरोध पूरा नहीं हो सकता. - अगर नतीजा मिलने से पहले आपका ऐप्लिकेशन बंद हो जाता है, तो सिस्टम नतीजे को सूची में डाल देता है. इसके बाद, जब आपका ऐप्लिकेशन अगली बार
registerForAllProfilingResultsके साथ ग्लोबल लिसनर को रजिस्टर करता है, तब नतीजा डिलीवर कर दिया जाता है.
- सफलता (
कस्टम ट्रेस पॉइंट जोड़ना: टाइमलाइन के हिस्सों को लेबल करने के लिए,
trace("...") { ... }(याTrace.beginSection()/Trace.endSection()) में ऑपरेशन रैप करें.
चल रही रिकॉर्डिंग को रोकना
इंटरैक्शन पूरा होते ही कैप्चर को रोकने के लिए, setDurationMs() के खत्म होने का इंतज़ार न करें. इसके बजाय, setCancellationSignal() या requestProfiling() को CancellationSignal पास करें. अगर आपने CancellationSignal और setDurationMs(), दोनों तय किए हैं, तो जो भी पहले होगा उससे सेशन खत्म हो जाएगा. अगर आपने दोनों में से कोई भी तय नहीं किया है, तो सिस्टम डिफ़ॉल्ट अवधि लागू करता है.
cancel() को कॉल करने पर, रिकॉर्डिंग खारिज नहीं होती. सिस्टम, डेटा कैप्चर करना बंद कर देता है. साथ ही, उस समय तक इकट्ठा किए गए डेटा को मिटाकर सेव कर देता है. इसके बाद, ProfilingResult को ERROR_NONE के साथ आपके लिसनर को डिलीवर कर देता है.
कॉल स्टैक सैंपलिंग की मदद से, सिस्टम ट्रेस रिकॉर्ड करना
Android 17 (एपीआई लेवल 37) और इसके बाद के वर्शन (या अपडेट किए गए Profiling मेनलाइन मॉड्यूल वाले Android 16) पर चलने वाले डिवाइसों पर, ProfilingManager एक ही सेशन में सिस्टम ट्रेस के साथ-साथ सैंपल किए गए कॉल स्टैक इकट्ठा करने की सुविधा देता है. इससे आपको टाइमलाइन इवेंट देखने में मदद मिलती है. जैसे, थ्रेड की स्थितियां, फ़्रेम, और कस्टम ट्रेस स्लाइस. साथ ही, उन तरीकों के लिए सीपीयू कॉल स्टैक भी देखे जा सकते हैं जिनमें साफ़ तौर पर ट्रेस इंस्ट्रुमेंटेशन नहीं है.
इस उदाहरण में, Jetpack बिल्डर के बिना ProfilingManager.requestProfiling को सीधे कॉल करने का तरीका भी बताया गया है. इसके लिए, इस्तेमाल किए जा सकने वाले प्रोफ़ाइल टाइप (PROFILING_TYPE_SYSTEM_TRACE) में से किसी एक को चुनें और कॉन्फ़िगरेशन Bundle पास करें:
Kotlin
val profilingManager = context.getSystemService(ProfilingManager::class.java) // Pass a Bundle to customize the capture, or pass null for default settings. val config = Bundle().apply { putInt("KEY_DURATION_MS", 15000) // 15 seconds putInt("KEY_SIZE_KB", 20480) // 20 MB buffer size // Collect sampled CPU call stacks alongside the system trace. putBoolean("KEY_COLLECT_STACK_SAMPLING", true) putInt("KEY_FREQUENCY_HZ", 100) // Sampling frequency in Hz // Optional: Set KEY_SAMPLE_BINDER_ONLY to true to sample only binder calls. } profilingManager?.requestProfiling( ProfilingManager.PROFILING_TYPE_SYSTEM_TRACE, config, "trace-with-callstacks", null, callbackExecutor, resultCallback )
Java
ProfilingManager profilingManager = context.getSystemService(ProfilingManager.class); if (profilingManager == null) return; // Pass a Bundle to customize the capture, or pass null for default settings. Bundle config = new Bundle(); config.putInt("KEY_DURATION_MS", 15000); // 15 seconds config.putInt("KEY_SIZE_KB", 20480); // 20 MB buffer size // Collect sampled CPU call stacks alongside the system trace. config.putBoolean("KEY_COLLECT_STACK_SAMPLING", true); config.putInt("KEY_FREQUENCY_HZ", 100); // Sampling frequency in Hz // Optional: Set KEY_SAMPLE_BINDER_ONLY to true to sample only binder calls. profilingManager.requestProfiling( ProfilingManager.PROFILING_TYPE_SYSTEM_TRACE, config, "trace-with-callstacks", null, callbackExecutor, resultCallback );
Perfetto यूज़र इंटरफ़ेस (यूआई) में, जनरेट की गई प्रोफ़ाइल खोलने पर, सैंपल किए गए कॉल स्टैक, आपके ऐप्लिकेशन के सामान्य ट्रेस स्लाइस के साथ-साथ, खास तौर पर बनाए गए कॉलस्टैक ट्रैक के तौर पर दिखते हैं. कॉल स्टैक ट्रैक का कोई हिस्सा चुनें, ताकि उस समयसीमा के लिए सैंपल किए गए कॉल स्टैक का फ़्लेम ग्राफ़ देखा जा सके. जैसा कि पहली इमेज में दिखाया गया है.
Java हीप डंप कैप्चर करना
JavaHeapDumpRequestBuilder (या PROFILING_TYPE_JAVA_HEAP_DUMP) का इस्तेमाल करके, अपने ऐप्लिकेशन की Java मेमोरी के स्नैपशॉट का अनुरोध किया जा सकता है.
Debug.dumpHprofData की मदद से कैप्चर किए गए स्टैंडर्ड .hprof हीप डंप के उलट, ProfilingManager हीप डंप को प्रोडक्शन में सुरक्षित और कम ओवरहेड वाले कलेक्शन के लिए डिज़ाइन किया गया है:
- नॉन-फ़्रीज़िंग कलेक्शन: सिस्टम, आपके ऐप्लिकेशन की प्रोसेस का कॉपी-ऑन-राइट क्लोन बनाता है और क्लोन की गई प्रोसेस से हीप स्नैपशॉट इकट्ठा करता है. इससे डिस्क में डंप लिखे जाने के दौरान, आपका ऐप्लिकेशन फ़्रीज़ नहीं होता.
- सिर्फ़ रेफ़रंस ग्राफ़ (कोई रॉ ऑब्जेक्ट डेटा नहीं):
ProfilingManager,.hprofफ़ाइल जैसे रॉ मेमोरी कॉन्टेंट को डंप करने के बजाय,.perfetto-java-heap-dumpprotobuf ग्राफ़ को रिकॉर्ड करता है. इसमें क्लास, ऑब्जेक्ट के साइज़, रेफ़रंस चेन, और गार्बेज कलेक्शन (जीसी) रूट शामिल होते हैं. हालांकि, इसमें प्रिमिटिव वैल्यू, स्ट्रिंग कॉन्टेंट, और कलेक्शन पेलोड शामिल नहीं होते. इससे उपयोगकर्ता की निजता सुरक्षित रहती है और फ़ाइल का साइज़ कम हो जाता है. पूरी जानकारी के लिए, देखें कि ProfilingManager के हीप डंप, HPROF डंप से किस तरह अलग हैं.
सिस्टम ट्रेस रिकॉर्ड करें में दिखाए गए Context, Executor, और resultCallback सेटअप का इस्तेमाल करके, JavaHeapDumpRequestBuilder के साथ अनुरोध बनाएं:
Kotlin
val heapDumpRequest = JavaHeapDumpRequestBuilder() .setTag("memory-leak-check") .build() requestProfiling( appContext, heapDumpRequest, callbackExecutor, resultCallback )
Java
ProfilingRequest heapDumpRequest = new JavaHeapDumpRequestBuilder() .setTag("memory-leak-check") .build(); Profiling.requestProfiling( getApplicationContext(), heapDumpRequest, callbackExecutor, resultCallback );
मेमोरी लीक का पता लगाने के लिए, Java हीप डंप का विश्लेषण करने के बारे में पूरी जानकारी पाने के लिए, केस स्टडी: ओओएम क्रैश को डीबग करना लेख पढ़ें.
अगले चरण
प्रोफ़ाइलें कैप्चर करने के बाद, adb का इस्तेमाल करके उन्हें स्थानीय तौर पर वापस पाएं या WorkManager का इस्तेमाल करके उन्हें अपने बैकएंड पर अपलोड करें.