1# Obtaining Reasons for Abnormal Application Exits 2 3If an application crashes and then restarts, you often need to know why it crashed and what the state was, such as the RSS and PSS values of the application memory and the time of the last exit. You can obtain the information from the **launchParam** parameter in the **OnCreate** lifecycle function of the UIAbility and UIExtensionAbility. You can use the information to analyze and improve the application experience, adjust service logic, and boost the application stability. 4 5## Constraints 6 7Only the [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) and [UIExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md) support the obtaining of the last exit reason. 8 9## Available APIs 10 11Read [API](../reference/apis-ability-kit/js-apis-app-ability-abilityConstant.md#launchparam) for the API reference. 12 13| **API** | **Description**| 14| -------- | -------- | 15| [LaunchParam](../reference/apis-ability-kit/js-apis-app-ability-abilityConstant.md#launchparam) | Parameters for starting an ability. The **lastExitReason**, **lastExitMessage**, and **lastExitDetailInfo** fields record the information about the last abnormal exit of the ability. | 16| [LastExitDetailInfo](../reference/apis-ability-kit/js-apis-app-ability-abilityConstant.md#lastexitdetailinfo18) | Detailed information about the last exit.| 17 18## How to Develop 19 201. Obtain the reason for the last exit. 21 22 Read the last exit information of the ability from **launchParam** of the **OnCreate** lifecycle function of the UIAbility class. 23 24 ```ts 25 import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit'; 26 27 const MAX_RSS_THRESHOLD: number = 100000; 28 const MAX_PSS_THRESHOLD: number = 100000; 29 30 function doSomething() { 31 console.log('do Something'); 32 } 33 34 function doAnotherThing() { 35 console.log('do Another Thing'); 36 } 37 38 class MyAbility extends UIAbility { 39 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 40 // Obtain the exit reason. 41 let reason: number = launchParam.lastExitReason; 42 let subReason: number = -1; 43 if (launchParam.lastExitDetailInfo) { 44 subReason = launchParam.lastExitDetailInfo.exitSubReason; 45 } 46 let exitMsg: string = launchParam.lastExitMessage; 47 48 if (launchParam.lastExitDetailInfo) { 49 // Obtain the information about the process where the ability was running before it exited. 50 let pid = launchParam.lastExitDetailInfo.pid; 51 let processName: string = launchParam.lastExitDetailInfo.processName; 52 let rss: number = launchParam.lastExitDetailInfo.rss; 53 let pss: number = launchParam.lastExitDetailInfo.pss; 54 // Obtain other information. 55 let uid: number = launchParam.lastExitDetailInfo.uid; 56 let timestamp: number = launchParam.lastExitDetailInfo.timestamp; 57 } 58 } 59 } 60 ``` 61 622. Perform service processing based on the last exit information. 63 64 - You can add different processing logic for different exit reasons. The following provides an example. 65 66 ```ts 67 if (reason === AbilityConstant.LastExitReason.APP_FREEZE) { 68 // The ability exited last time due to no response. Add processing logic here. 69 doSomething(); 70 } else if (reason === AbilityConstant.LastExitReason.SIGNAL && subReason === 9) { 71 // The ability exited last time because the process is terminated by the kill -9 signal. Add processing logic here. 72 doAnotherThing(); 73 } else if (reason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) { 74 // The ability exited last time due to RSS control last time. Implement the processing logic here. The simplest approach is to print the information. 75 console.log('The ability has exit last because the rss control, the lastExitReason is '+ reason + 76 ', subReason is ' + subReason + ', lastExitMessage is ' + exitMsg); 77 } 78 ``` 79 80 - Detect abnormal application memory usage based on process information. The following provides an example. 81 82 ```ts 83 if (rss > MAX_RSS_THRESHOLD || pss > MAX_PSS_THRESHOLD) { 84 // If the RSS or PSS value is too high, the memory usage is close to or has reached the upper limit. Print a warning or add processing logic. 85 console.warn('Process ' + processName + '(' + pid + ') memory usage approaches or reaches the upper limit.'); 86 } 87 ``` 88 89 - Use the timestamp of the abnormal exit to pinpoint when the issue occurred, facilitating problem locating. 90 91 ```ts 92 console.log('App ' + uid + ' terminated at ' + timestamp); 93 ``` 94