当前位置:网站首页>node中,如何手动实现触发垃圾回收机制
node中,如何手动实现触发垃圾回收机制
2022-04-23 17:20:00 【草样的年华】
参考阮一峰ES6标准入门中的实例,手动实现
node --expose-gc // --expose-gc 表示允许手动执行垃圾回收机制
然后执行代码:
// 手动执行一次垃圾回收保证内存数据准确
> global.gc();
undefined
// 查看当前占用的内存,主要关心heapUsed字段,大小约为4.4MB
> process.memoryUsage();
{ rss: 21626880,
heapTotal: 7585792,
heapUsed: 4708440,
external: 8710 }
// 创建一个WeakMap
> let wm = new WeakMap();
undefined
// 创建一个数组并赋值给变量key
> let key = new Array(1000000);
undefined
// 将WeakMap的键名指向该数组
// 此时该数组存在两个引用,一个是key,一个是WeakMap的键名
// 注意WeakMap是弱引用
> wm.set(key, 1);
WeakMap { [items unknown] }
// 手动执行一次垃圾回收
> global.gc();
undefined
// 再次查看内存占用大小,heapUsed已经增加到约12MB
> process.memoryUsage();
{ rss: 30232576,
heapTotal: 17694720,
heapUsed: 13068464,
external: 8688 }
// 手动清除变量key对数组的引用
// 注意这里并没有清除WeakMap中键名对数组的引用
> key = null;
null
// 再次执行垃圾回收
> global.gc()
undefined
// 查看内存占用大小,发现heapUsed已经回到了之前的大小(这里约为4.8M,原来为4.4M,稍微有些浮动)
> process.memoryUsage();
{ rss: 22110208,
heapTotal: 9158656,
heapUsed: 5089752,
external: 8698 }
版权声明
本文为[草样的年华]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42690194/article/details/124259852
边栏推荐
- Promise (IV)
- Shell-入门、变量、以及基本的语法
- Further study of data visualization
- Solution architect's small bag - 5 types of architecture diagrams
- If you start from zero according to the frame
- EF core in ASP Generate core priority database based on net entity model
- Document operation II (5000 word summary)
- 文件操作《二》(5000字总结篇)
- Use of Shell sort command
- . net cross platform principle (Part I)
猜你喜欢
Net standard
How to change input into text
Document operation II (5000 word summary)
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Shell script -- shell programming specification and variables
[registration] tf54: engineer growth map and excellent R & D organization building
Detailed explanation of Milvus 2.0 quality assurance system
XTask与Kotlin Coroutine的使用对比
[WPF binding 3] listview basic binding and data template binding
RPC核心概念理解
随机推荐
RPC核心概念理解
Handwritten event publish subscribe framework
XTask与Kotlin Coroutine的使用对比
AIOT产业技术全景结构-数字化架构设计(8)
Paging SQL
Further optimize Baidu map data visualization
Devexpress GridView add select all columns
[related to zhengheyuan cutting tools]
Further study of data visualization
JSON deserialize anonymous array / object
[problem solving] [show2012] random tree
Use between nodejs modules
Router object, route object, declarative navigation, programmed navigation
Shell-sort命令的使用
ASP. Net core dependency injection service life cycle
Use of shell sed command
Milvus 2.0 质量保障系统详解
Input file upload
Shortcut keys (multiline)
Generate random numbers with high quality and Gaussian distribution