Try   HackMD

#Session 4 Memory Leak 與你的距離

歡迎來到 JSDC 2021 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

共筆入口:https://hackmd.io/@JSDC-tw/2021conference
手機版請點選上方 按鈕展開議程列表。

共筆協作,共享知識,打造開放社群。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Welcome to JSDC 2021 Collaborative Writing

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Collaborative Writing Workplace:https://hackmd.io/@JSDC-tw/2021conference
Using mobile please tap to unfold the agenda.

Build a community with open collaboration.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

從這開始
可以直接打字做共筆囉

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Kuan@方格子

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Memory Leak 是什麼

  • Memory: 儲存各種東東的地方
    • 結構
      • stack
        • 我們要有 Release
      • heap
        • 新資料

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用 Grafana 觀察

Outline

  • Memory Leak 病徵病因
  • Garbage Collection in V8
  • DevTool

Memory Leak 病徵病因

複習 Stack and Heap

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Heap
    • Code Space: 編譯後的程式碼

(演示)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • notive 推入 global stack

  • character 是由 makeWizard 決定

    • 呼叫 makeWizard 函式
      • 將 name 推到 makeWizard 的 stack
      • 將 wizard 推到 makeWizard 的 stack
      • return 傳給呼叫他的人
    • character(ptr) 進入 stack
      • 指向 heap 的 character 值
        • 補充:因為 character 很大,而且可能會到處傳(複製開銷大,學 rust 可以幫助了解這些知識)
  • 若改呼叫 makeWizard

    • wizard 還會存活在 heap 裡面嗎?
      • wizard 會消失
    • who did that?
      • GC
        • Image Not Showing Possible Reasons
          • The image file may be corrupted
          • The server hosting the image is unavailable
          • The image path is incorrect
          • The image format is not supported
          Learn More →

Garbage Collection in V8

GC三階段

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Defragment
    • Windows 硬碟重組
      • SSD 用 TRIM 不要 Defrag!

Two collectors

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

https://v8.dev/blog/orinoco-parallel-scavenger

  • Young Generation
    • Nursery
      • 剛出現的新東西
    • Intermediate
  • Old Generation
    • 比較不需要被清掉

Why should GC Generation be existed?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

semi-space design
from-space and to-space in young

Major GC

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Sweep 成本很高,一般V8不會頻繁進行整理,V8判定為 reachable 會保留 反之回收掉

Find Memory Leak in App

下述有 Memory Leak

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

使用 node inspect 來 Debug
在 DevTools > Memory

使用 k6 進行壓力測試
做之前先進行 snapshot

文字版

https://vocus.cc/article/61176c17fd89780001942f1c