当前位置:网站首页>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
边栏推荐
- Summary of common websites
- Further optimize Baidu map data visualization
- [ES6] promise related (event loop, macro / micro task, promise, await / await)
- Collect blog posts
- 1-2 JSX syntax rules
- Baidu Map Case - modify map style
- Paging SQL
- Seven cattle upload pictures (foreground JS + background C API get token)
- Manually implement call, apply and bind functions
- ASP. Net core configuration options (Part 2)
猜你喜欢

Devexpress GridView add select all columns

If you start from zero according to the frame
![Using quartz under. Net core - [1] quick start](/img/80/b99417e88d544ca6e3da4c0c1625ce.png)
Using quartz under. Net core - [1] quick start

Shell script -- shell programming specification and variables

【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
![[difference between Oracle and MySQL]](/img/90/6d030a35692fa27f1a7c63985af06f.png)
[difference between Oracle and MySQL]

Net standard

Use between nodejs modules

Understanding of RPC core concepts

groutine
随机推荐
Shell - introduction, variables, and basic syntax
Promise (IV)
Some problems encountered in recent programming 2021 / 9 / 8
Multithreaded @ async thread pool
Baidu Map 3D rotation and tilt angle adjustment
Shell-sort命令的使用
Using quartz under. Net core - [1] quick start
1-2 characteristics of nodejs
[markdown notes]
Use of shell sed command
Conversion between hexadecimal numbers
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Kingdee Cloud Star API calling practice
Seven cattle upload pictures (foreground JS + background C API get token)
El cascade and El select click elsewhere to make the drop-down box disappear
ASP. NET CORE3. 1. Solution to login failure after identity registers users
Shell-入门、变量、以及基本的语法
Simulation of infrared wireless communication based on 51 single chip microcomputer
【WPF绑定3】 ListView基础绑定和数据模板绑定
ASP. Net core reads the configuration file in the class library project