V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
rabbbit
V2EX  ›  问与答

Vue 的 keep-alive 会导致内存泄漏?

  •  
  •   rabbbit · 2019-04-07 15:14:06 +08:00 · 3206 次点击
    这是一个创建于 2012 天前的主题,其中的信息可能已经有所发展或是发生改变。

    具体表现为切换路由时,exclude 中不需要缓存的 component 无法被释放,还保存在内存中.

    keep-alive 代码如下

    <keep-alive :include="['shop']" :exclude="['album']">
      <router-view ref="router"></router-view>
    </keep-alive>
    

    include 中的 shop 是一个需要被缓存的页面

    exclude 中的 album 是一个不需要被缓存

    在 shop 和 album 多次切换后, 可以发现 heap snapshot 中有大量没有释放的 vue component.

    初始的 snapshot 1

    多次切换路由后的 snapshot 2, 可以看到大量未释放的 component

    我找到了一个相关的issue.
    但是官方认为这个 bug 已经被修复了

    有人遇到过这个问题吗,有什么解决办法吗?

    第 1 条附言  ·  2019-04-09 11:17:02 +08:00
    第 2 条附言  ·  2021-04-23 09:01:14 +08:00
    该 bug 已被修复 https://github.com/vuejs/vue/pull/12015
    将会在版本 2.6.13 发布
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3270 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:21 · PVG 19:21 · LAX 04:21 · JFK 07:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.