Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/keepalive-transition-memory-leak.md
1---2title: KeepAlive with Transition Memory Leak3impact: MEDIUM4impactDescription: Combining KeepAlive with Transition can cause memory leaks in certain Vue versions5type: gotcha6tags: [vue3, keepalive, transition, memory-leak, animation]7---89# KeepAlive with Transition Memory Leak1011**Impact: MEDIUM** - There is a known memory leak when using `<Transition>` and `<KeepAlive>` together. Component instances may not be properly freed from memory when combining these features.1213## Task Checklist1415- [ ] Test memory behavior when using KeepAlive + Transition together16- [ ] Consider if transition animation is necessary with cached components17- [ ] Use browser DevTools Memory tab to verify no leak18- [ ] Keep Vue updated to get latest bug fixes1920## The Problem2122```vue23<template>24<!-- Known memory leak combination in some Vue versions -->25<Transition name="fade">26<KeepAlive>27<component :is="currentView" />28</KeepAlive>29</Transition>30</template>31```3233When switching between components repeatedly:34- Component instances accumulate in memory35- References prevent garbage collection36- Memory usage grows with each switch3738## Diagnosis3940Use Chrome DevTools to detect the leak:41421. Open DevTools > Memory tab432. Take heap snapshot443. Switch between components 10+ times454. Take another heap snapshot465. Compare: look for growing VueComponent count4748## Workarounds4950### Option 1: Remove Transition if Not Essential5152```vue53<template>54<!-- No memory leak without Transition -->55<KeepAlive :max="5">56<component :is="currentView" />57</KeepAlive>58</template>59```6061### Option 2: Use CSS Animations Instead6263```vue64<template>65<KeepAlive :max="5">66<component67:is="currentView"68:class="{ 'fade-enter': isTransitioning }"69/>70</KeepAlive>71</template>7273<style>74.fade-enter {75animation: fadeIn 0.3s ease-in;76}7778@keyframes fadeIn {79from { opacity: 0; }80to { opacity: 1; }81}82</style>83```8485### Option 3: Use Strict Cache Limits8687If you must use both, minimize impact with strict limits:8889```vue90<template>91<Transition name="fade" mode="out-in">92<KeepAlive :max="3">93<component :is="currentView" />94</KeepAlive>95</Transition>96</template>97```9899### Option 4: Key-Based Cache Invalidation100101Force fresh instances when needed:102103```vue104<script setup>105import { ref, computed } from 'vue'106107const currentView = ref('Dashboard')108const cacheKey = ref(0)109110function switchViewFresh(view) {111currentView.value = view112cacheKey.value++ // Force new instance113}114</script>115116<template>117<Transition name="fade" mode="out-in">118<KeepAlive :max="3">119<component :is="currentView" :key="cacheKey" />120</KeepAlive>121</Transition>122</template>123```124125## Keep Vue Updated126127This is a known issue tracked in Vue's GitHub repository. Memory leak fixes are periodically released, so ensure you're on the latest Vue version:128129```bash130npm update vue131```132133## Key Points1341351. **Known issue** - Memory leaks with KeepAlive + Transition are documented1362. **Test in DevTools** - Use Memory tab to verify your specific usage1373. **Consider alternatives** - CSS animations may work without the leak1384. **Set strict `max`** - Limit cache size to cap memory impact1395. **Keep Vue updated** - Bug fixes are released periodically140141## Reference142- [GitHub Issue #9842: Memory leak with transition and keep-alive](https://github.com/vuejs/vue/issues/9842)143- [GitHub Issue #9840: Memory leak with transition and keep-alive](https://github.com/vuejs/vue/issues/9840)144- [Vue.js KeepAlive Documentation](https://vuejs.org/guide/built-ins/keep-alive.html)145