(Recommended) Proactively release nodes when creating custom nodes in the component to prevent memory leakage caused by unreleased nodes.
// code-linter.json5
{
"rules": {
"@performance/custom-node-memory-leak-check": "warn",
}
} import { BuilderNode } from '@kit.ArkUI';
@Entry
@Component
struct BuilderNodeDisposeExample {
private builder: BuilderNode<[]> | null = null
build() {
Column({ space: 20 }) {
Button('open dialog')
.onClick(() => {
const uiContext = this.getUIContext()
this.builder = new BuilderNode(uiContext) // Create a BuilderNode.
})
Button('close dialog')
.onClick(() => {
if (this.builder) {
this.builder.dispose() // Release the constructed node.
this.builder = null
}
})
}
.width('100%')
.height('100%')
.padding(20)
.backgroundColor(Color.Grey)
}
} import { BuilderNode } from '@kit.ArkUI';
@Entry
@Component
struct LeakyBuilderExample {
private builder: BuilderNode<[]> | null = null
build() {
Column({ space: 20 }) {
Button('create dialog')
.onClick(() => {
const context = this.getUIContext();
// Create a new builder without releasing the old builder.
this.builder = new BuilderNode(context)
})
}
.width('100%')
.height('100%')
.padding(20)
.backgroundColor(Color.Grey)
}
} plugin:@performance/all
For details about how to configure Code Linter check rules, see Code Linter.