3

请教 nodejs 中数据占用内存的计算方法

 2 years ago
source link: https://www.v2ex.com/t/845559
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Node.js

请教 nodejs 中数据占用内存的计算方法

  slmaaw · 3 小时 45 分钟前 via Android · 318 次点击
有一个空对象{}或者 map ,向其中塞入以下 kv:

key 是 uuid<string>

value 是时间戳数字<number>(类似 Date.now())

请问塞入 100 万个的话,这个对象或者 map 会占用多大内存?
5 条回复    2022-04-07 23:37:22 +08:00

eason1874

eason1874      3 小时 39 分钟前

1GB 以上吧,你本地写段代码真塞 100 万数据进去不就知道了

eason1874

eason1874      3 小时 36 分钟前

不对,脑子短路了,算错了,应该不到 100MB

noe132

noe132      3 小时 35 分钟前

不同引擎实现不同占用的内存也不同。
不过可以大概估算,uuid 就是个 36 字符的字符串,number 如果格式统一就是个 int32 。如果没有介入优化,通常就是一个大数组,那么就是字符串+int32 的大小 x 1m 。如果介入优化,还需要加上 hashtable 的大小。

secondwtq

secondwtq      51 分钟前

Chrome 的 Web Inspector 里面有一个 Memory Tab ,可以分析内存占用。

试了一下:
const l = 1000000, data = {}
for (let i = 0; i < l; i++) data[i.toString().padStart(36, '0')] = Date.now()+i;

key 约占 48MB ,也就是说一个 string 48 字节,value 占 12MB ,一个 number 12 字节,再加上外面约 25MB ,一个键值对应该是 24 字节差不多

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK