Fix memory leak in preview code (#2012)

Co-authored-by: filtered <176114999+webfiltered@users.noreply.github.com>
This commit is contained in:
AustinMroz
2024-12-22 15:18:31 -06:00
committed by GitHub
parent 3767749398
commit cce5ade578
2 changed files with 22 additions and 0 deletions

View File

@@ -1547,6 +1547,7 @@ export class ComfyApp {
api.addEventListener('executing', ({ detail }) => {
this.progress = null
this.graph.setDirtyCanvas(true, false)
this.revokePreviews(this.runningNodeId)
delete this.nodePreviewImages[this.runningNodeId]
})
@@ -1589,6 +1590,8 @@ export class ComfyApp {
const blob = detail
const blobUrl = URL.createObjectURL(blob)
// Ensure clean up if `executing` event is missed.
this.revokePreviews(id)
this.nodePreviewImages[id] = [blobUrl]
})
@@ -2835,11 +2838,24 @@ export class ComfyApp {
app.graph.setDirtyCanvas(true, true)
}
/**
* Frees memory allocated to image preview blobs for a specific node, by revoking the URLs associated with them.
* @param nodeId ID of the node to revoke all preview images of
*/
revokePreviews(nodeId: NodeId) {
if (!this.nodePreviewImages[nodeId]?.[Symbol.iterator]) return
for (const url of this.nodePreviewImages[nodeId]) {
URL.revokeObjectURL(url)
}
}
/**
* Clean current state
*/
clean() {
this.nodeOutputs = {}
for (const id of Object.keys(this.nodePreviewImages)) {
this.revokePreviews(id)
}
this.nodePreviewImages = {}
this.lastNodeErrors = null
this.lastExecutionError = null