Skip to content

Memory

onMemoryWarning

TIP

The API usage is as follows: wx.onMemoryWarning(function listener)

  • Functional description: Listen for low memory warning events. This event is triggered when iOS/Android issues a memory warning to the Mini Program process. Triggering this event does not mean that the Mini Program process is cleared. In most cases, it is just a warning. Developers can recycle some unnecessary resources after receiving the notification to avoid further aggravating memory shortages.

  • Parameters and descriptions: function listener,Listener function for low memory warning event

    PropertiesTypeDescription
    levelnumberMemory warning level, only available on Android, corresponding to system macro definition
  • level legal value

    Legal valueDescription
    5TRIM_MEMORY_RUNNING_MODERATE
    10TRIM_MEMORY_RUNNING_LOW
    15TRIM_MEMORY_RUNNING_CRITICAL
  • Sample code:

js
wx.onMemoryWarning(function () {
  console.log('onMemoryWarningReceive')
})

offMemoryWarning

TIP

The API usage is as follows: wx.offMemoryWarning(function listener)

  • Functional description: Remove the listener function for low memory warning events

  • Parameters and descriptions: function listener,Listener function passed in by onMemoryWarning. If this parameter is not passed, all listeners will be removed

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onMemoryWarning(listener)
wx.offMemoryWarning(listener) // Pass the same function object used for listening.