Memory Optimization

View Markdown

Last edited on 2026-07-28

1. Rational Use of Subpackage Loading

Using Subpackage Loading not only optimizes startup time but also enables coarser-grained on-demand loading of pages, components, and logic, thereby reducing memory usage. For details, please refer to 《Startup Optimization - Code Package Size Optimization》.

2. Using On-Demand Injection and On-Use Injection

By enabling On-Demand Injection and On-Use Injection, you can avoid loading unused pages and components at runtime, thereby reducing memory consumption. For more details, please refer to 《Launch Optimization - Code Injection Optimization》.

3. Memory Analysis

For a more granular analysis of the memory distribution in the Mini Program logic layer, you can use the "Memory Debug" feature provided by the Memory Debugging option in the Developer Tools debugger or the "Memory Debugging" capability offered by Real Device Debugging 2.0.

4. Handling Memory Warnings

If Mini Program consumes excessive system resources, it may be terminated by the system or actively reclaimed by the WeChat client. On iOS, when the WeChat client receives consecutive system memory warnings within a specific time interval, it will proactively terminate the Mini Program based on defined strategies and notify the user with the message: "Insufficient running memory. Please reopen this Mini Program."

It is recommended that Mini Program use wx.onMemoryWarning to listen for memory warning events and perform necessary memory cleanup when needed. For example: releasing components or JS objects that are temporarily unused.

5. Mini Program Common Memory Leak Issues

Memory leak issues cause Mini Program memory usage to continuously increase during execution, leading to Mini Program crashes or forced termination by WeChat.

5.1 Mini Program Long-term retention of page instances prevents proper destruction of the page instance and its referenced components

After the page unload event occurs, the base library cleans up the page instance from the page stack. Under normal circumstances, the JavaScript garbage collection mechanism reclaims the page and frees memory.

However, if the page instance (this) held in the developer's code is not released, the page will not be properly reclaimed, causing memory leaks. Developers are advised to be aware of this issue and perform necessary cleanup in the unload lifecycle hook.

Case 1: Page instance referenced by unbound event listeners

If event listeners hold a reference to the page's this and are not unbound after the page is destroyed, the page cannot be released.

Page({
 themeChangeHandler({theme}) {
 this.setData({theme})
},
 onLoad() {
 this._handler = this.themeChangeHandler.bind(this)
 wx.onThemeChange(this._handler)
},
 // Fix: Unbind the listener in unload
 // onUnload() {
 // wx.offThemeChange(this._handler)
 //},
})

Case 2: Page instance referenced by external or global variables

If a function closure holds a reference to the page's this, and that function is assigned to a global variable or a variable outside the page lifecycle, the page cannot be released.

let languageListener = null

Page({
 onLoad() {
 getApp().userInfoChangeListener = ({userName}) => {
 this.setData({userName})
}
 languageListener = ({lang}) => {
 this.setData({lang})
}
},
 // Fix: Clean up in unload
 // onUnload() {
 // getApp().userInfoChangeListener = null
 // languageListener = null
 //},
})

Case 3: Page instance referenced for a long duration by asynchronous callbacks

If the page's this is accessed within asynchronous callbacks that do not return for a long time, such as excessive setTimeout or setInterval durations, or long-running wx API callbacks (like a prolonged wx.request), the page cannot be released.

Page({
 onLoad() {
 this._timer = setInterval(() => {
 this.setData({
 timerValue: Date.now()
})
}, 1000)
},
 // Fix: Clean up in unload
 // onUnload() {
 // clearInterval(this._timer)
 //},
})

5.2 Event listeners not unbound in a timely manner

After event listening ends, the listener should be unbound promptly.

const locationChangeListener = function (res) {
  console.log('location change', res)
}
wx.onLocationChange(locationChangeListener)
wx.startLocationUpdate()
// After listening ends
wx.stopLocationUpdate()
// Fix: Unbind the listener promptly when no longer in use
// wx.offLocationChange(locationChangeListener)

5.3 Uncleared Timers

When developing features such as "flash sale countdowns," developers may use setInterval to set timers. Before a page or component is destroyed, the clearInterval method must be called to cancel the timer.