• 受 V8 内存限制,在Node中通过JS使用内存时会收到限制,老生代 64系统下约1.4GB,32系统下约为0.7G,

新生代 64系统下最大值 32MB,32系统下最大值 16MB(node高版本不一定是这个限制,node12 64位测 试老生代约2G)

  • process.memeryUsage() 返回当前 V8 内存使用情况(字节为单位)

    1. {
    2. rss: 23429120, // 驻留集大小, 给该进程分配的物理内存大小(包含堆,栈,和代码段)
    3. heapTotal: 553369, // 已申请到的堆内存
    4. heapUsed: 2813888 // 已使用的堆内存
    5. extranal: 1301672 // 绑定到js的C++对象的内存使用情况
    6. }

    如果已申请的堆空闲内存不够分配新对象就将继续申请内存,直到超过V8限制为止

  • 可以通过 node 启动时传递 —max-old-space-size 或 —max-new-space-size 来调整默认限制从而使用更多内存

    1. node --max-old-space-size=1700 test.js // 单位MB,为老生代区分配 1700 MB 空间,默认1400MB
    2. node --max-new-space-size=1024 test.js // 单位KB,为新生代区分配 1024 KB 空间
  • 可以通过在启动时添加 —trace_gc 参数来收集垃圾回收日志

    1. node --trace_gc -e "var a=[];for(var i=0;i<1973050;i++){a.push(new Array(128).fill(0))}" > gc.log

    JS数字占 8Byte,1288B = 1KB 1KB 1973050 = 1.88GB,上述代码还不会 out of memery
    从 log 可以看出 v8 垃圾回收对新生代使用 Scavenge 算法,老生代使用 Mark-Sweep 算法

  • 可以通过在启动时添加 —prof 参数可以收集 V8 运行时性能分析数据

    1. node --prof test.js

    这会生成一个 v8.log 文件,需要用 linux-tick-processor 工具查看

  • 测试V8内存代码 ``javascript // test.js const os = require("os"); function showMemo() { const mem = process.memoryUsage(); function format(bytes) { return (bytes / 1024 / 1024).toFixed(2) + "MB"; } console.log(Process: heapTotal ${format(mem.heapTotal)} heapUsed ${format(

    1. mem.heapUsed

    )} rss ${format(mem.rss)} free ${format(os.freemem())}` ); console.log(“—————————————-“); }

function useMem() { var size = 0.125 1024 1024; return new Array(size).fill(0); // 1MB }

var total = []; for (let index = 0; index < 15; index++) { showMemo(); total.push(useMem()); } showMemo();

// 执行 node test.js 会发生 OOM 崩溃

Process: heapTotal 4.78MB heapUsed 2.18MB rss 19.63MB free 4096.12MB

Process: heapTotal 165.28MB heapUsed 162.52MB rss 180.38MB free 3932.46MB

Process: heapTotal 326.29MB heapUsed 322.30MB rss 340.97MB free 3932.46MB

Process: heapTotal 488.79MB heapUsed 482.33MB rss 501.17MB free 3932.46MB

Process: heapTotal 652.79MB heapUsed 642.30MB rss 661.39MB free 3932.46MB

Process: heapTotal 820.80MB heapUsed 802.30MB rss 821.66MB free 3932.46MB

Process: heapTotal 996.80MB heapUsed 962.30MB rss 982.11MB free 3932.46MB

Process: heapTotal 1156.80MB heapUsed 1122.30MB rss 1142.21MB free 3932.46MB

Process: heapTotal 1316.81MB heapUsed 1282.30MB rss 1302.22MB free 2814.63MB

Process: heapTotal 1477.31MB heapUsed 1442.33MB rss 1462.25MB free 2655.65MB

Process: heapTotal 1637.32MB heapUsed 1602.30MB rss 1622.26MB free 2496.98MB

Process: heapTotal 1797.32MB heapUsed 1762.30MB rss 1782.27MB free 2341.26MB

Process: heapTotal 1957.32MB heapUsed 1922.30MB rss 1942.38MB free 2180.51MB

Process: heapTotal 2116.33MB heapUsed 2081.77MB rss 2102.44MB free 2018.95MB

<—- Last few GCs —->

// 执行 node —max-old-space-size=4048 test.js 不会崩溃了

Process: heapTotal 4.78MB heapUsed 2.15MB rss 20.06MB free 3848.58MB

Process: heapTotal 165.28MB heapUsed 162.48MB rss 180.79MB free 3683.48MB

Process: heapTotal 326.29MB heapUsed 322.27MB rss 341.39MB free 3522.06MB

Process: heapTotal 488.79MB heapUsed 482.30MB rss 501.59MB free 3522.06MB

Process: heapTotal 652.79MB heapUsed 642.27MB rss 661.77MB free 3522.06MB

Process: heapTotal 820.80MB heapUsed 802.27MB rss 822.05MB free 3522.06MB

Process: heapTotal 996.80MB heapUsed 962.27MB rss 982.59MB free 3522.06MB

Process: heapTotal 1156.80MB heapUsed 1122.27MB rss 1142.63MB free 3522.06MB

Process: heapTotal 1316.81MB heapUsed 1282.27MB rss 1302.70MB free 2556.02MB

Process: heapTotal 1476.81MB heapUsed 1442.27MB rss 1462.70MB free 2396.52MB

Process: heapTotal 1636.82MB heapUsed 1602.28MB rss 1622.82MB free 2235.52MB

Process: heapTotal 1796.82MB heapUsed 1762.28MB rss 1782.83MB free 2075.02MB

Process: heapTotal 1956.82MB heapUsed 1922.28MB rss 1942.86MB free 1915.11MB

Process: heapTotal 2116.83MB heapUsed 2082.28MB rss 2102.86MB free 1754.66MB

Process: heapTotal 2276.83MB heapUsed 2242.28MB rss 2262.87MB free 1754.66MB

Process: heapTotal 2436.84MB heapUsed 2402.28MB rss 2422.88MB free 1754.66MB

  1. - Buffer 占用的内存属于堆外内存,不受 V8 限制,上述用例,改成 Buffer 就不会 OOM,由此,善用 Buffer 是可以规避 V8 内存限制
  2. ```javascript
  3. function useMem() {
  4. var size = 200 * 1024 * 1024;
  5. return Buffer.alloc(size).fill(0); // 200MB
  6. }
  7. // 执行 node test.js 不会发生 OOM 崩溃,且 heapTotal几乎无变化,RSS 变化较大
  8. Process: heapTotal 4.78MB heapUsed 2.18MB rss 19.48MB free 3902.73MB
  9. ---------------------------
  10. Process: heapTotal 5.28MB heapUsed 2.52MB rss 220.32MB free 3698.29MB
  11. ---------------------------
  12. Process: heapTotal 5.28MB heapUsed 2.52MB rss 420.36MB free 3497.33MB
  13. ---------------------------
  14. Process: heapTotal 5.28MB heapUsed 2.53MB rss 620.37MB free 3296.33MB
  15. ---------------------------
  16. Process: heapTotal 5.78MB heapUsed 1.78MB rss 820.54MB free 3095.83MB
  17. ---------------------------
  18. Process: heapTotal 5.78MB heapUsed 1.79MB rss 1021.95MB free 2895.83MB
  19. ---------------------------
  20. Process: heapTotal 5.78MB heapUsed 1.79MB rss 1221.98MB free 2695.33MB
  21. ---------------------------
  22. Process: heapTotal 5.78MB heapUsed 1.79MB rss 1421.98MB free 2695.33MB
  23. ---------------------------
  24. Process: heapTotal 7.78MB heapUsed 1.77MB rss 1622.14MB free 2695.33MB
  25. ---------------------------
  26. Process: heapTotal 7.78MB heapUsed 1.77MB rss 1822.14MB free 2695.33MB
  27. ---------------------------
  28. Process: heapTotal 7.78MB heapUsed 1.77MB rss 2022.15MB free 1893.91MB
  29. ---------------------------
  30. Process: heapTotal 7.78MB heapUsed 1.77MB rss 2222.16MB free 1692.91MB
  31. ---------------------------
  32. Process: heapTotal 7.78MB heapUsed 1.77MB rss 2422.16MB free 1492.41MB
  33. ---------------------------
  34. Process: heapTotal 7.78MB heapUsed 1.77MB rss 2622.16MB free 1291.91MB
  35. ---------------------------
  36. Process: heapTotal 7.78MB heapUsed 1.77MB rss 2822.16MB free 1091.41MB
  37. ---------------------------
  38. Process: heapTotal 7.78MB heapUsed 1.78MB rss 3022.16MB free 891.41MB
  39. ---------------------------
  40. // 把循环次数由15改成100,发现也不会OOM,只是 RSS 值到达极限值时不会再变化
  41. Process: heapTotal 5.28MB heapUsed 1.77MB rss 9283.31MB free 16.89MB
  42. ---------------------------
  43. Process: heapTotal 5.28MB heapUsed 1.76MB rss 9283.50MB free 16.20MB
  44. ---------------------------
  45. Process: heapTotal 5.28MB heapUsed 1.76MB rss 9284.76MB free 15.18MB
  46. ---------------------------
  47. Process: heapTotal 5.28MB heapUsed 1.76MB rss 9283.68MB free 16.57MB
  48. ---------------------------
  49. Process: heapTotal 5.28MB heapUsed 1.77MB rss 9284.70MB free 15.57MB
  50. ---------------------------
  51. Process: heapTotal 5.28MB heapUsed 1.76MB rss 9283.72MB free 15.57MB
  52. ---------------------------
  53. Process: heapTotal 5.28MB heapUsed 1.76MB rss 9283.37MB free 15.57MB
  54. ---------------------------
  55. Process: heapTotal 5.28MB heapUsed 1.77MB rss 9284.91MB free 14.22MB
  56. ---------------------------
  • 内存泄漏

造成内存泄漏一般有三个原因:作用域未释放、缓存无过期策略、队列消费不及时

  • 内存泄漏排查工具:node-heapdump、node-memwatch
  • 测试内存泄漏代码