当前位置:网站首页>Node memory overflow and V8 garbage collection mechanism
Node memory overflow and V8 garbage collection mechanism
2022-07-26 09:38:00 【Bean sprouts don't eat beans】
First send the address of the boss's article to express your gratitude :https://www.jianshu.com/p/74a466789ff4
Problem recurrence , The title and code are as follows
// Given an array of integers nums And a target value target, Please find and as the target value in the array Two Integers , And return their array subscripts .
//
// You can assume that each input corresponds to only one answer . however , The same element in an array cannot be used twice .
//
//
// Example :
//
// Given nums = [2, 7, 11, 15], target = 9
// because nums[0] + nums[1] = 2 + 7 = 9
// So back [0, 1]
var twoSum = function(nums, target) {
var result = [];
var num1;
var num2
nums.map((item,index)=>{
for(let i = index+1;i<nums.length;i++){
var sum = item+nums[i];
result.push(sum);
if(sum === target) {
num1= index;
num2 = i;
}
}
});
return [num1,num2]
};
console.log(twoSum([1,2,6,7,8],15)) // [3,4]
When a length of 12599 The array of , Error report, memory overflow :
Reasons for appearance :
js The object of is stored in heap memory , stay node By default, there is a limit to the memory used in ,64 Bit system is probably 1.4GB,32 The bit system is probably 0.7GB. Here, the array length of the submitted code is 12599, According to my code result The length is 79361101. In about 75… It's a mistake , out of memory .
terms of settlement
1. Modify the code , Don't just result Just fine , Originally result It doesn't help ~~
var twoSum = function(nums, target) {
var num1;
var num2
nums.map((item,index)=>{
for(let i = index+1;i<nums.length;i++){
var sum = item+nums[i];
if(sum === target) {
num1= index;
num2 = i;
}
}
});
return [num1,num2]
};
2. Since it is a memory overflow , Can you change the memory size ?
Sure enough, Du Niang knows everything , Modify the command as follows :
node --max-new-space-size=1024 app.js // Unit is KB Set the maximum value of the new generation memory
node --max-old-space-size=2000 app.js // Unit is MB Set the maximum memory of the old generation
About the new generation and the old generation, this involves V8 The garbage collection mechanism of !!
V8 Garbage collection mechanism
Generational garbage collection mechanism , That is to say v8 According to the life cycle of the object, the memory is divided into two kinds of memory space: the new generation and the old generation , Then use more efficient algorithms for different memory .
The new generation (New Space): Save objects with a short lifetime , In the Cenozoic, mainly through Scavenge The algorithm divides the memory space into from and to Two spaces , When allocating objects, start with from The distribution of , When garbage collection starts , take from The surviving objects in are copied to to Memory block , Then free the memory space of non living objects . After copying , The roles of the two spaces are interchanged , To recycle .
Old generation (Old Space): Save long-lived or resident objects . In the old generation Mark-sweep Algorithm ( Mark clear ) Follow Mark-Compat Algorithm ( Tag to sort out ) Combined way to garbage collection .
Mark-sweep** First traverse all objects and mark the surviving objects , In the subsequent clearing phase, only objects outside the mark are cleared .
shortcoming : After a garbage collection , Memory becomes fragmented , Unable to allocate a large object memory .
It's a combination of Mark-Compat Algorithm It's perfect : The algorithm After marking the living object , In the process of finishing , Move all living objects to one end , Clean up the memory outside the boundary directly after the move , This avoids the problem of memory fragmentation .
Can the objects with long life cycle in the new generation go to the old generation ??
Old generation theory :“ Think about it , Let me see if you have been Scavenge This guy recycled , To judge where you are (from Space is still to Space ). Secondly, I have to see your to The proportion of space and memory exceeds 25%, If it exceeds you, you don't have to from to Back and forth , Come directly to me , You will only occupy space blindly there , Affect the little brother's subsequent memory allocation .”
边栏推荐
猜你喜欢

一种分布式深度学习编程新范式:Global Tensor

2019 ICPC Asia Yinchuan regional (water problem solution)

配置ADCS后访问certsrv的问题

解决npm -v突然失效 无反应
![[Online deadlock analysis] by index_ Deadlock event caused by merge](/img/67/0a02ad248c3ab21d3240e12aa23313.png)
[Online deadlock analysis] by index_ Deadlock event caused by merge

QT handy notes (III) use qtcharts to draw a line chart in VS

Login module use case writing

服务器、客户端双认证(2)

Fuzzy PID control of motor speed

在Blazor 中自定义权限验证
随机推荐
【荧光字效果】
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
JS judge the data types object.prototype.tostring.call and typeof
Antd treeselect gets the value of the parent node
附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”
Audio and video knowledge
E. Two Small Strings
MySql5.7.25源码安装记录
wap端微信h5支付,用于非微信浏览器
莫队学习笔记(一)
Network flow learning notes
注册模块用例编写
dll中的全局变量
Node 内存溢出及V8垃圾回收机制
v-premission添加权限
Interview shock 68: why does TCP need three handshakes?
MFC handy notes
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
服务器环境配置全过程
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)