Skip to content

Memory leak when using "transition" and "keep-alive" #9842

Closed
@Aaron-Bird

Description

@Aaron-Bird

Version

2.6.10

Reproduction link

https://codepen.io/aaronbird/pen/JVRKwa

Steps to reproduce

I wrote a demo:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>demo</title>
    <style>
      .link {
        cursor: pointer;
        user-select: none;
      }
    </style>
  </head>
  <body>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>

    <div id="app">
      <transition>
        <keep-alive include="foo">
          <component :is="target" @change="onChange"></component>
        </keep-alive>
      </transition>
    </div>
    <script>
      const Foo = {
        name: "foo",
        template: `<div class="link" @click="$emit('change', 'bar')">Go to Bar</div>`
      };
      const Bar = {
        name: "bar",
        template: `<div class="link" @click="$emit('change', 'foo')">Go to Foo</div>`
      };
      Vue.component("foo", Foo);
      Vue.component("bar", Bar);
      const app = new Vue({
        data: {
          target: "foo"
        },
        methods: {
          onChange(target) {
            this.target = target;
          }
        }
      }).$mount("#app");
    </script>
  </body>
</html>

The debugging process is as follows:
1 Click "Collect garbage" and "Take heap snapshot"(snapshot 1)
2 Click the routing button on the left 10 times, then click "Collect garbage" and "Take heap snapshot"(snapshot 2)
3 Click the routing button on the left 10 times, then click "Collect garbage" and "Take heap snapshot"(snapshot 3)

The generated "snapshot" is as follows:

snapshot 1:

snapshot 2:

snapshot 3:

After clicking 100 times:

VueComponent is not being recycled, is this a bug?

What is expected?

Why is this happening, is this a bug?

What is actually happening?

Component will not be cleared when switching components.

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