Skip to content

throttle 依然存在内存泄漏 #73

@sakura-flutter

Description

@sakura-flutter

可复现的链接:

问题描述与截图:

看过这个 PR,但 3.7.6 版本仍然会有泄漏:
image

问题出在目前只在 runFn 执行时调用了 gcFn,如果在 wait 内执行多次且 trailing 为 false 那么总会有上一次的 argscontext 不能释放

var gcFn = function () {
args = null
context = null
}
var runFn = function () {
runFlag = true
callback.apply(context, args)
timeout = setTimeout(endFn, wait)
gcFn()
}
var endFn = function () {
timeout = null
if (!runFlag && optTrailing === true) {
runFn()
}
}

vxe-core 监听了 wheel 事件,在 100ms 内快速滚动鼠标滚轮,就会导致内存泄漏,除非手动再滚动一次才能释放 throttle 上次持有的对象

window.addEventListener(wheelName, XEUtils.throttle(triggerEvent, 100, { leading: true, trailing: false }), { passive: true, capture: false })

或许以下的方式可以修复这个问题,在 endFn 里 gc 一下

 var endFn = function () {
    timeout = null
    if (!runFlag && optTrailing === true) {
      runFn()
    }
+   gcFn()
  }

期望的结果:

正常释放内存

操作系统:

Windows 11

浏览器版本:

138.0.7204.101

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions