当前位置:网站首页>【AntV G2】如何解决 G2 造成的内存泄露
【AntV G2】如何解决 G2 造成的内存泄露
2022-07-17 00:14:00 【yulamz】
如何解决 G2 造成的内存泄露
简介
在容器节点被销毁时,应调用 chart.destroy() 以销毁实例释放资源,避免内存泄漏。
内存泄露的基本示例
在一个 Vue 组件中使用 G2 库而没有将其及时清除导致的内存泄漏。假设页面中存在多个标签页,每个标签页都包含一些图表。当选中一个标签页的时候,其他标签页的内容在 DOM 中被移除了。这样,当用户再选中这些标签页的时候,就会发现图表被清空了。本质上,这是由于图表的容器节点被移除导致的。即使之后该节点被重新添加,图表所在的节点也已经不存在了。
这个示例的问题在于 Vue 的相关指令会从 DOM 中移除父级元素,却不会移除由 G2 新添加的 DOM 片段,需要我们手动清除这部分,否则将会导致内存泄漏。
解决这个内存泄漏的问题
在上述的示例中,我们可以在标签页的内容在 DOM 中被移除之前做一些清理工作,来解决内存泄漏问题。为了做到这一点,我们可以将清理工作放入 Vue 的 beforeDestroy() 生命周期钩子里,并会使用 G2 API 中的 destroy() 方法将其清除。
<div class="gcharts2" :id="id"></div>
import {
Chart } from '@antv/g2'
export default {
data () {
return {
chart: null,
charData: [
{
year: '1991', value: 3 },
{
year: '1992', value: 4 },
{
year: '1993', value: 3.5 },
{
year: '1994', value: 5 }
]
}
},
props: {
id: {
type: String,
default: ''
}
},
mounted () {
if (this.id) {
this.drawChart()
}
},
beforeDestroy () {
this.chart.destroy()
},
methods: {
drawChart () {
this.chart = new Chart({
container: this.id,
autoFit: true,
height: 400
})
// 此处省略载入图表数据源,使用图形语法进行图表绘制的代码
this.chart.render()
}
}
}
这样做的价值
内存管理和性能测试在快速交付的时候是很容易被忽视的,然而,保持小内存开销仍然对整体的用户体验非常重要。良好的内存管理实践会避免糟糕的浏览器崩溃的场景,同时也解决了潜在的性能恶化问题。
总结
使用 Vue 时需要警惕内存泄漏,这些内存泄漏往往会发生在使用 Vue 之外的其它进行 DOM 操作的三方库(如 G2 图表库)时。请确保测试应用的内存泄漏问题并在适当的时机做必要的组件清理。
正确的做法是,在图表容器被销毁之后,调用 chart.destroy() 销毁实例,在图表容器重新被添加后再次调用 new Chart() 创建 Chart 图表对象。
本文同时贡献在 Github 的开源项目 G2-50-Questions
边栏推荐
- RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
- 浏览器无法打开Tensorboard
- [unity panel attribute literacy] set texture import settings after importing textures
- LeetCode:动态规划【基础题目求解】
- Hash table, bloom filter, distributed consistency hash
- VIM profile
- ENVI_IDL:批量拼接Modis Swath的逐日数据并输出为Geotiff格式
- 【工具篇】Unity屏幕画线,Unity屏幕画图HSJ绘画工具
- Gdb+vscode debugging -- how to debug the release version
- Hue oozie editor scheduling shell
猜你喜欢
![[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d](/img/84/34879d5c0b6e19d599c99eec944c26.png)
[unity Editor Extension] the pre-processing and post-processing pictures of unity assets are automatically transferred to sprite2d

散列表、布隆过滤器、分布式一致性hash

Swagger——世界上最流行的Api框架

ENVI_IDL:读取OMI数据(HDF5)并输出为Geotiff文件+详细解析

VIM profile

STL--queue容器

ENVI_IDL:批量重投影Modis Swath产品并指定范围输出为Geotiff格式+解析
![[hdrp HD rendering pipeline] create hdrp project and upgrade the built-in pipeline project to hdrp project](/img/40/d3b2bb62db88c7b18a3c3df4df5609.png)
[hdrp HD rendering pipeline] create hdrp project and upgrade the built-in pipeline project to hdrp project

LeetCode:动态规划【基础题目求解】

ENVI_IDL:讀取所有OMI產品的NO2柱含量並計算月均值、季均值、年均值+解析
随机推荐
Recursive and recursive learning notes
[tools] unity2d character controller, which controls 2D players to move and jump in four directions and horizontal directions
Lintcode 366:Fibonacci 斐波那契数列
【Unity编辑器扩展】快速定位资源和脚本的指定文件和路径
Oozie integrated sqoop
[unity panel attribute literacy] set texture import settings after importing textures
windows安装mysql和jdbc
VS Code 问题:launch:program‘...\.vscode\launch.exe‘ dose not exist
ENVI_IDL:批量对Modis Swath产品进行均值运算+解析
Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】
The code of dsaa related articles in the blog
LeetCode:动态规划【基础题目求解】
Leetcode 1: Two Sum
STL--set容器
【HSJFramework】Unity时间管理TimeManger计时器
捉虱子的博弈论
ENVI_ Idl: average calculation + analysis of MODIS swath products in batches
散列表、布隆过滤器、分布式一致性hash
[unity Editor Extension] scriptableobject for internal asset configuration of unity
Chengxin University envi_ IDL third week class content 1: reading OMI data (HDF5 file) and output + parsing