To obtain memory information of the current application, perform the following steps:

Generally, you only need to note the data in the Pss Total column, that is, the physical memory actually used by the application. As shown in the following figure, the application occupies a total of 26,279 KB memory, including 4,712 KB of the ArkTS heap (ArkTS heap memory) and 13,164 KB of the native heap.

You can use the @ohos.hidebug (HiDebug) API to obtain memory information of an application process. For details, see Obtaining Memory Information.
HarmonyOS provides the onMemoryLevel() API for you to listen for the system memory changes and adjust the application memory usage accordingly. The onMemoryLevel() API has three modes: AbilityStage, UIAbility, and EnvironmentCallback.
MemoryLevel can be set to MEMORY_LEVEL_MODERATE, MEMORY_LEVEL_LOW, or MEMORY_LEVEL_CRITICAL. MEMORY_LEVEL_MODERATE indicates that the current memory capacity is moderate, and the application can run properly with performance not compromised. MEMORY_LEVEL_LOW indicates that the current memory capacity is low, and the application should release unnecessary memory resources to avoid system freezing. MEMORY_LEVEL_CRITICAL indicates that the remaining memory capacity is insufficient, and applications should release resources as much as possible to ensure system stability and performance. You should take measures based on different memory levels, such as releasing resources and optimizing memory usage, to ensure that applications can run properly in different memory states. The following table defines MemoryLevel.
Level | Value | Description |
|---|---|---|
MEMORY_LEVEL_MODERATE | 0 | The system memory capacity is moderate. The system may start to kill the process based on the LRU cache rule. |
MEMORY_LEVEL_LOW | 1 | The system memory capacity is low. You need to release some unnecessary resources to improve the system performance. |
MEMORY_LEVEL_CRITICAL | 2 | The system memory capacity is insufficient. You need to release unnecessary resources if possible because the system may kill all processes in the cache and start to kill the processes that should be running, such as the services running in the background. |
If an application has been frozen in the background, onMemoryLevel of AbilityStage, UIAbility, and EnvironmentCallback cannot be called back.