- API 路由统一: 移除 /api/h5/,统一 /api/pms/ 入口 - 二进制改名: api-server -> pms-api(与服务名一致) - nginx: 合并 /api/ + /pms/api/ 为 /api/pms/ - 新增 docs/API.md 接口文档 - 后端: CabinetDetail 增加 rssi/pow_fail/address/soc 等字段 - Redis 管道批量读取仓体实时数据(SOC/电压/电流/功率) - DB: 所有 TIMESTAMP 列改为 DATETIME(修复类型不匹配) - 用户管理: 新增创建时间/修改时间列,状态改用 Switch - 角色管理/用户管理: 操作按钮改为图标 - PC 设备详情: 基本信息补全 RSSI/ICCID/电源/地址 - PC 卡片: 通道统计改用 Lucide 图标,颜色统一 - PC 二维码: canvas 渲染,含编号文字,支持右键下载 - H5: 按 uni-app 规范修正导航(navigateTo 替代 reLaunch) - H5: 详情页加返回导航条 - H5: 扫码支持摄像头(jsQR)+ ?id= 参数 - H5: 底部 tabBar 在所有页面常显 - H5: 首页统计改为 6 项(设备/通道/空闲/充电/充满/故障) - H5: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
4.9 KiB
4.9 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| KeepAlive Memory Management - Prevent Memory Leaks | HIGH | Unbounded KeepAlive caching can cause severe memory issues, especially with large or numerous components | gotcha |
|
KeepAlive Memory Management - Prevent Memory Leaks
Impact: HIGH - KeepAlive caches component instances in memory. Without proper limits and cleanup, this can lead to memory exhaustion, especially in applications with many routes or large component trees.
Task Checklist
- Always use the
maxprop to limit cached instances - Clean up resources in
onDeactivatedhook - Monitor memory usage when using KeepAlive extensively
- Be cautious with KeepAlive on memory-heavy components
- Test on memory-constrained devices (mobile, low-spec laptops)
The Problem: Unbounded Cache Growth
<template>
<!-- DANGEROUS: No limit on cached components -->
<KeepAlive>
<component :is="currentView" />
</KeepAlive>
</template>
When users navigate through many different components, each instance stays in memory:
- Chrome memory grows continuously
- VueComponent count keeps increasing
- App becomes sluggish or crashes
Solution: Set Cache Limits
<template>
<!-- LRU cache: keeps max 10, evicts least recently used -->
<KeepAlive :max="10">
<component :is="currentView" />
</KeepAlive>
</template>
How max works:
- KeepAlive uses an LRU (Least Recently Used) cache algorithm
- When cache exceeds
max, the oldest unused component is destroyed - This caps memory usage to a predictable maximum
Choose max Based on Your Use Case
<template>
<!-- Tab-based UI: usually 3-5 tabs max -->
<KeepAlive :max="5">
<component :is="currentTab" />
</KeepAlive>
<!-- Route-based caching: limit to frequently visited pages -->
<router-view v-slot="{ Component }">
<KeepAlive :max="3" include="Dashboard,Settings,Profile">
<component :is="Component" />
</KeepAlive>
</router-view>
</template>
Clean Up Resources in Deactivated Hook
Even with max, cached components hold resources. Clean up when deactivated:
<script setup>
import { ref, onActivated, onDeactivated, onUnmounted } from 'vue'
const chartInstance = ref(null)
const websocket = ref(null)
const intervalId = ref(null)
onActivated(() => {
// Resume or recreate resources
websocket.value = new WebSocket('...')
intervalId.value = setInterval(fetchData, 5000)
})
onDeactivated(() => {
// IMPORTANT: Clean up to reduce memory footprint while cached
chartInstance.value?.destroy()
chartInstance.value = null
websocket.value?.close()
websocket.value = null
clearInterval(intervalId.value)
intervalId.value = null
})
// Final cleanup when truly destroyed
onUnmounted(() => {
// Same cleanup for when component is evicted from cache
chartInstance.value?.destroy()
websocket.value?.close()
clearInterval(intervalId.value)
})
</script>
Third-Party Library Cleanup
Libraries that manipulate the DOM outside Vue need explicit cleanup:
<script setup>
import { ref, onMounted, onDeactivated, onUnmounted } from 'vue'
const mapContainer = ref(null)
let mapInstance = null
onMounted(() => {
mapInstance = new MapLibrary(mapContainer.value)
})
onDeactivated(() => {
// Some map libraries hold significant memory
// Destroy and recreate on reactivation if needed
mapInstance?.remove()
mapInstance = null
})
onUnmounted(() => {
mapInstance?.remove()
mapInstance = null
})
</script>
Avoid KeepAlive for Memory-Heavy Components
Some components should NOT be cached:
<script setup>
const heavyComponents = ['VideoPlayer', 'LargeDataGrid', 'MapView']
</script>
<template>
<!-- Exclude memory-heavy components from cache -->
<KeepAlive :exclude="heavyComponents" :max="5">
<component :is="currentView" />
</KeepAlive>
</template>
Monitor Memory in Development
<script setup>
import { onActivated, onDeactivated } from 'vue'
if (import.meta.env.DEV) {
onActivated(() => {
console.log('Activated - Memory:', performance.memory?.usedJSHeapSize)
})
onDeactivated(() => {
console.log('Deactivated - Memory:', performance.memory?.usedJSHeapSize)
})
}
</script>
Key Points
- Always set
max- Never use KeepAlive without a reasonable limit - Clean up in
onDeactivated- Don't wait for unmount to release resources - Exclude heavy components - Large data grids, media players, maps
- Test on target devices - Mobile users have less memory
- Monitor in development - Watch for growing memory usage