go gc algorithm 101

0db20c33cdd9bed3a7191d07a1c51c14?s=47 taxio
October 28, 2019

go gc algorithm 101

Go Conference 2019 Autumn

0db20c33cdd9bed3a7191d07a1c51c14?s=128

taxio

October 28, 2019
Tweet

Transcript

  1. ©2019 Wantedly, Inc. Go GC algorithm 101 Go Conference 2019

    Autumn Oct 28, 2019 - Takuma Shiotsu(@taxio_tech)
  2. ©2019 Wantedly, Inc. ࣗݾ঺հ ‣ Wantedly. Inc. 20ଔ಺ఆऀ ‣ ژ౎޻ܳણҡେֶେֶӃ

    M2 - ιϑτ΢ΣΞ޻ֶݚڀࣨ 5BLVNB4IJPUTV !UBYJP@UFDI
  3. ©2019 Wantedly, Inc. ✋($Λগ͠Ͱ΋஌͍ͬͯΔਓ✋

  4. ©2019 Wantedly, Inc. ✋ฉ͍ͨ͜ͱ΋ͳ͍ͱ͍͏ਓ✋

  5. ©2019 Wantedly, Inc. ✋࣮૷ͨ͜͠ͱ͕͋Δਓ✋

  6. ©2019 Wantedly, Inc. ຊηογϣϯͷ໨ඪ w ($ͱ͸Կ͔Λ஌Δ w ϓϩϑΝΠϧ݁ՌΛோΊ͍ͯΔͱ͖ʹͳΜͱͳ͘ SVOUJNFͷ($ϑΣʔζΛײ͡औΕΔΑ͏ʹͳΔ w

    3FMFBTF/PUFͷ3VOUJNFͷ߲໨Λ ϫΫϫΫ͠ͳ͕ΒோΊΔ͖͔͚ͬΛ࣋ͭ
  7. ©2019 Wantedly, Inc. ँࣙ w ($ʹؔ͢Δ·ͱ·ͬͨ৘ใ͸ຊ౰ʹগͳ͍ ‣ 3JDIBSE+POFT FUD ʮΨϕʔδίϨΫγϣϯࣗಈϝϞϦ؅ཧΛ

    ߏ੒͢Δཧ࿦ͱ࣮૷ʯ ‣ தଜ੒༸ FUD ʮΨϕʔδίϨΫγϣϯͷΞϧΰϦζϜͱ࣮૷ʯ w ॻ੶΍ϒϩάΛެ։͍ͯͩͬͯ͘͠͞Δํʑɼ ͦͯ͠ԿΑΓ(PͷDPOUSJCVUPSͷํʑʹਂ͘ײँ͠·͢
  8. ©2019 Wantedly, Inc. "HFOEB w ࿩͢͜ͱ ‣ ($ͷجૅతͳཧ࿦ ‣ (PͰ࠾༻͞Ε͍ͯΔ($ΞϧΰϦζϜͷભҠͱ಺༰

    w ࿩͞ͳ͍͜ͱ ‣ (P͕࠾༻͍ͯ͠ͳ͍ଞͷ($ΞϧΰϦζϜ ‣ ώʔϓߏ଄ɼ"MMPDBUJPO΍&TDBQFղੳͷ࿩
  9. ©2019 Wantedly, Inc. "HFOEB w ࠓ೔ͷηογϣϯͷྲྀΕ ‣ ($ͷجૅͷઆ໌ ‣ (Pͱ.BSL4XFFQ($

    ‣ (Pͱ$PODVSSFOU.BSL4XFFQ($ w ཧ࿦࣮૷ཧ࿦࣮૷ͷॱͰઆ໌͍͖ͯ͠·͢
  10. ©2019 Wantedly, Inc. *OUSPEVDUJPO

  11. ©2019 Wantedly, Inc. ($ͱ͸ʁ w (BSCBHF$PMMFDUJPO w ώʔϓྖҬʹ֬อ͞Εͨɼ ΋͏࢖ΘΕͳ͍Ͱ͋Ζ͏ϝϞϦΛղ์͢Δ w

    (PͰ͸3VOUJNFʹ౥ࡌ͞Ε͍ͯΔ
  12. ©2019 Wantedly, Inc. (PͰ͸ w ҰఆͷλΠϛϯάͰ3VOUJNF͕($Λىಈ w runtime.GC()Ͱ΋ىಈՄೳ w ($ʹӨڹΛ༩͑Δ؀ڥม਺

    ‣ GOGCɼGODEBUGʹ৭ʑ ‣ IUUQTRJJUBDPNNBUUOJUFNTFDGG
  13. ©2019 Wantedly, Inc. ($ͷجૅ༻ޠ

  14. ©2019 Wantedly, Inc. ($ͷجૅ༻ޠ w ΦϒδΣΫτ ‣ ώʔϓྖҬ͔Β֬อ͢Δ σʔλߏ଄ͷ୯Ґ ‣

    ϔομΦϒδΣΫτͷϝλ৘ใ ‣ ϑΟʔϧυ஋΍ଞͷΦϒδΣΫτͷࢀর w ϛϡʔςʔλ ‣ ΦϒδΣΫτͷੜ੒ "MMPDBUJPO ΍ ߋ৽Λߦ͏ )FBEFS 'JFMET
  15. ©2019 Wantedly, Inc. ($ͷجૅ༻ޠ w 3PPU ‣ ΦϒδΣΫτͷࢀরؔ܎Λ௥͏ͨΊͷ࢝఺ ‣ TUBDL΍TUBUJDྖҬʹอ࣋͞ΕΔม਺ͳͲ

    ‣ 3PPUʹଘࡏ͢ΔΦϒδΣΫτΛ3PPUΦϒδΣΫτͱݴ͏ w ࢮΜͰ͍ΔΦϒδΣΫτ ‣ 3PPU͔ΒḷΕͳ͍ΦϒδΣΫτ͸΋͏࢖ΘΕͳ͍͸ͣ  ͜ΕΛճऩ͍ͨ͠ %FBE 3PPU
  16. ©2019 Wantedly, Inc. ͭ·Γ w ($ΞϧΰϦζϜͱ͍͏ͷ͸ɼ ‣ ࢮΜͰ͍Δੜ͖͍ͯΔΦϒδΣΫτΛͲ͏ݟ෼͚ͯɼ Ͳ͏ղ์͍ͯ͘͠ͷ͔ ‣

    ಈ͖ଓ͚ΔϛϡʔςʔλͱͲ͏͍͏࿈ܞΛऔΔͷ͔ w ධՁ࣠ ‣ -BUFODZϛϡʔςʔλͷ ࠷େ ఀࢭ࣌ؒ ‣ 5ISPVHIQVU୯Ґ࣌ؒ͋ͨΓͷώʔϓαΠζॲཧೳྗ ‣ ଞʹ΋ώʔϓྖҬͷঢ়ଶʹ͍͔ؔͯͭ͘͠
  17. ©2019 Wantedly, Inc. ۩ମతͳ($ΞϧΰϦζϜ

  18. ©2019 Wantedly, Inc. .BSL4XFFQ($ w ೥ʹൃද͞Εͨ࠷ॳͷ($ΞϧΰϦζϜ ‣ +PIO.D$BSUIZ 3FDVSTJWFGVODUJPOTPGTZNCPMJDFYQSFTTJPOTBOE UIFJSDPNQVUBUJPOCZNBDIJOF

    $PNNVOJDBUJPOPGUIF"$. "QS w ͭͷϑΣʔζ͔Β੒Δ ‣ ੜ͖͍ͯΔΦϒδΣΫτΛ୳͢.BSLϑΣʔζ ‣ ࢮΜͰ͍ΔΦϒδΣΫτΛճऩ ղ์ ͢Δ4XFFQϑΣʔζ w ($͕ىಈ͔ͯ͠Βऴྃ͢Δ·Ͱɼϛϡʔςʔλ͸ಈ࡞͠ͳ͍ ‣ 458 4UPQ5IF8PSME  ‣ ͜Ε͕MBUFODZͷओͳཁҼ
  19. ©2019 Wantedly, Inc. .BSLϑΣʔζ w ੜ͖͍ͯΔΦϒδΣΫτΛݟ͚ͭΔϑΣʔζ w 3PPU͔Β୳ࡧ͍͖ͯ͠ɼ ݟ͚ͭͨΦϒδΣΫτͷϔομʹ.BSLΛ෇͚͍ͯ͘ ✔

    ✔ ✔ Root
  20. ©2019 Wantedly, Inc. 4XFFQϑΣʔζ w ࢮΜͰ͍ΔΦϒδΣΫτΛճऩ͢ΔϑΣʔζ w ώʔϓྖҬΛ୺͔Β୳ࡧ w .BSLͷ෇͍͍ͯͳ͍ΦϒδΣΫτΛճऩ

    ‣ ࣍ͷ"MMPDBUJPOʹ࢖͏ͨΊͷ४උ ‣ ྫ͑͹GSFFͳPCKFDUΛ؅ཧ͓ͯ͘͠Ϧετʹ௥Ճ ✔ ✔ ✔ Heap
  21. ©2019 Wantedly, Inc. .BSL4XFFQ($ w ϝϦοτ ‣ ୯७Ͱൺֱత࣮૷͕؆୯ w σϝϦοτ

    ‣ ϝϞϦͷஅยԽ ϑϥάϝϯςʔγϣϯ  ‣ ௿଎ͳ"MMPDBUJPO ‣ 458ͷӨڹͰMBUFODZ͕େ͖͍
  22. ©2019 Wantedly, Inc. (Pͱ.BSL4XFFQ($ w (P·Ͱओʹ͜ͷΞϧΰϦζϜ w લճͷ($ͰNBSLͨ͠ੜଘΦϒδΣΫτͷ૯αΠζ  

    GOGC ͷϝϞϦ͕৽͘͠"MMPDBUF͞Εͨͱ͖ʹىಈ ‣ NBSLࡁΈ૯αΠζͷॳظ஋͸.# ‣ GOGC=offͷ৔߹($͸ىಈ͠ͳ͍ w ΋ͪΖΜ͜Ε͚ͩͩͱ஗͍ͷͰɼ ͍ͭ͘΋޻෉͕੝Γࠐ·Ε͍ͯΔ ‣ #JUNBQNBSLJOH 1BSBMMFMJNQM 5$.BMMPD FUD
  23. ©2019 Wantedly, Inc. #JUNBQNBSLJOH w ΦϒδΣΫτͷϔομʹ.BSLΛ෇͚ΔͷͰ͸ͳ͘ɼ ผͷྖҬʹ·ͱΊ͓ͯ͘ ‣ 4XFFQ࣌ͷ૸͕ࠪ଎͘ͳΔ ‣

    .BSLϑϥάΛ໭͢ͷָ͕ ‣ $PQZPO8SJUFͱͷ૬ੑ͕͍͍
  24. ©2019 Wantedly, Inc. ͔͠͠458ͷӨڹͰ MBUFODZ͕େ͖͍

  25. ©2019 Wantedly, Inc. ͦ΋ͦ΋ͳͥ458͕ඞཁʁ w ($தʹϛϡʔςʔλ͕ΦϒδΣΫτͷࢀরߏ଄Λม͑ͯ ͠·͏ͱɼ੔߹ੑΛอূͰ͖ͳ͍ ྫ͑͹...

  26. ©2019 Wantedly, Inc. $PODVSSFOU .BSL4XFFQ($

  27. ©2019 Wantedly, Inc. $PODVSSFOU.BSL4XFFQ($ w ϛϡʔςʔλͱ($Λฒߦʹಈ࡞ͤ͞Δ w ຬͨ͢΂͖ਖ਼౰ੑ ‣ গͳ͘ͱ΋3PPU͔Β౸ୡՄೳͳશͯͷPCKFDU͕

    ࣦΘΕͳ͍Α͏ʹ͢Δ ‣ ࠷ऴతʹ($Λ׬ྃ͢Δ w Ͳ͏΍ͬͯ͜ΕΒΛຬͨ͠ͳ͕Βฒߦ࣮ߦ͢Δͷ͔ʁ ‣ ৭NBSLJOH ‣ 8SJUFCBSSJFS
  28. ©2019 Wantedly, Inc. ৭NBSLJOH w PCKFDUʹͭͷঢ়ଶΛఆٛ ศ্ٓɼ৭Ͱදݱ  ‣ നະ୳ࡧ

    NBSLແ͠  ‣ փ୳ࡧத NBSLࡁΈϦετʹೖΕ͓ͯ͘  ‣ ࠇ୳ࡧࡁΈ NBSLࡁΈϦετʹແ͍  w Ϧετ͔ΒΦϒδΣΫτͷࢀরΛऔΓग़͠ɼ ͦͷGJFMEΛ୳ࡧ͍ͯ͘͠ ‣ (PͰ͸RVFVFͱ࣮ͯ͠૷͞Ε͍ͯΔ Root
  29. ©2019 Wantedly, Inc. 8SJUFCBSSJFS w ΦϒδΣΫτ͕໎ࢠʹͳΔ৚݅ ‣ ϛϡʔςʔλ͕നΦϒδΣΫτ΁ͷϙΠϯλΛ ࠇΦϒδΣΫτʹॻ͖ࠐΉ 

    શͯͷփ৭ΦϒδΣΫτ͔Βͷɼ ͦͷനΦϒδΣΫτ΁ͷܦ࿏͕ଘࡏ͠ͳ͍ w ͜ͷ৚͕݅ୡ੒͞Εͳ͍Α͏ʹ ϛϡʔςʔλͷΦϒδΣΫτૢ࡞ʹҰఆͷॲཧΛڬΉ ‣ ৭ʑͳख๏͕͋Δ ‣ (P͕࠾༻͍ͯ͠Δख๏͸ޙͰղઆ
  30. ©2019 Wantedly, Inc. $PODVSSFOU.BSL4XFFQ($ w େ఍ɼҎԼͷϑΣʔζ͔Β੒Δ ‣ .BSLTFUVQ 458 

     3PPUΦϒδΣΫτΛશͯփ৭ʹృΔ  SVOUJNFͷ($ʹඞཁͳ͋Ε͜ΕͷॳظԽ ‣ $PODVSSFOUNBSL ‣ .BSLUFSNJOBUJPO 458   ඞཁͳ৔߹͸3PPUΛ࠶૸ࠪ ‣ $PODVSSFOUTXFFQ ‣ 4XFFQUFSNJOBUJPO 458
  31. ©2019 Wantedly, Inc. $PODVSSFOU.BSL4XFFQ($ w ϝϦοτ ‣ 458࡟ݮʹΑͬͯMBUFODZ͕ݮΔ w σϝϦοτ

    ‣ 8SJUFCBSSJFSʹΑͬͯεϧʔϓοτ͕Լ͕Δ
  32. ©2019 Wantedly, Inc. (PͰͷ࣮૷

  33. ©2019 Wantedly, Inc. (Pͷ$PODVSSFOU($࣮૷ͷྺ࢙ w "VH (P (BSCBHF$PMMFDUJPO ($ 1MBOBOE3PBENBQ

    ‣ IUUQTEPDTHPPHMFDPNEPDVNFOUE :*TO/3$/.Y/;D:9;-PWS)WW-I,@I,/XP50 w (P ‣ $PODVSSFOU4XFFQ w (P ‣ $PODVSSFOU.BSL 8SJUFCBSSJFS ‣ $PODVSSFOU($׬੒ w (P ‣ 8SJUFCBSSJFSվྑ Ͱվྑ׬ྃ   1SPQPTBMFMJNJOBUFSFTDBO
  34. ©2019 Wantedly, Inc. $PODVSSFOU($ಋೖ݁Ռ w MBUFODZ͕େ෯ʹݮͬͨ 'SPN3JDL)VETPO IUUQTUBMLTHPMBOHPSHHPHDQEG Q (PQIFS$PO%FOWFS

    +VMZ 
  35. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w "MMPDBUJPO࣌ ‣ ৽֬͘͠อ͞ΕͨΦϒδΣΫτΛ௚ͪʹࠇʹృΔ Root ৽ΦϒδΣΫτ

  36. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w "MMPDBUJPO࣌ ‣ ৽֬͘͠อ͞ΕͨΦϒδΣΫτΛ௚ͪʹࠇʹృΔ Root ৽ΦϒδΣΫτ

  37. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w ΦϒδΣΫτͷࢀরؔ܎Λॻ͖׵͑ͨͱ͖ ‣ ࢀর͞Ε͍ͯͨ͞Ε͍ͯΔΦϒδΣΫτͷ྆ํΛփ৭ʹృΔ ‣ ͢ͰʹNBSL͞Ε͍ͯΔ

    փPSࠇ ৔߹͸Կ΋͠ͳ͍ Ͳ͔͔͜Βͷࢀর
  38. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w ΦϒδΣΫτͷࢀরؔ܎Λॻ͖׵͑ͨͱ͖ ‣ ࢀর͞Ε͍ͯͨ͞Ε͍ͯΔΦϒδΣΫτͷ྆ํΛփ৭ʹృΔ ‣ ͢ͰʹNBSL͞Ε͍ͯΔ

    փPSࠇ ৔߹͸Կ΋͠ͳ͍ Ͳ͔͔͜Βͷࢀর
  39. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w ΦϒδΣΫτͷࢀরؔ܎Λॻ͖׵͑ͨͱ͖ ‣ ࢀর͞Ε͍ͯͨ͞Ε͍ͯΔΦϒδΣΫτͷ྆ํΛփ৭ʹృΔ ‣ ͢ͰʹNBSL͞Ε͍ͯΔ

    փPSࠇ ৔߹͸Կ΋͠ͳ͍ Ͳ͔͔͜Βͷࢀর
  40. ©2019 Wantedly, Inc. (Pͷ8SJUFCBSSJFS w ($։࢝࣌ʹࢮΜͰ͍ΔΦϒδΣΫτΛճऩ͢Δ ‣ ($ऴྃ࣌ʹࢮΜͰ͍ΔΦϒδΣΫτ͕ଘࡏ͠ͳ͍อূ͸ແ͍ ‣ %JKLTUSBTUZMFͱ:VBTBTUZMFͷϋΠϒϦου

    w SFGHJUIVCDPNHPMBOHHPCMPCFBTSD SVOUJNFNCBSSJFSHP
  41. ©2019 Wantedly, Inc. (Pͷ$PODVSSFOU($ w ϑΣʔζ  4XFFQUFSNJOBUJPO  .BSLTFUVQ

     .BSL  .BSLUFSNJOBUJPO  4XFFQ w SFGHJUIVCDPNHPMBOHHPCMPCFBTSD SVOUJNFNHDHP
  42. ©2019 Wantedly, Inc. 4XFFQUFSNJOBUJPO w લճͷ4XFFQ͕ऴΘ͍ͬͯͳ͍৔߹͸ɼ 458Λͨ͠͏͑Ͱ4XFFQϑΣʔζΛऴΘΒͤΔ

  43. ©2019 Wantedly, Inc. .BSLTFUVQ w 458 w ॳظԽ ‣ gcphase_GCoff_GCmark

    ‣ 8SJUFCBSSJFS༗ޮԽ ‣ "TTJTU༗ޮԽ w 3PPUΦϒδΣΫτΛશͯփ৭ʹృΔ
  44. ©2019 Wantedly, Inc. .BSL w ($༻ͷHPSPVUJOF͕NBSLJOH͍ͯ͘͠ w .BSLJOHΑΓ"MMPDBUJPOͷ଎౓͕ૣ͍৔߹ɼ "TTJTU༻ͷHPSPVUJOF͕ىಈ͢Δ

  45. ©2019 Wantedly, Inc. .BSLUFSNJOBUJPO w 458 w NBSLϑΣʔζͷޙॲཧ ‣ gcphase_GCmark_GCmarktermination

    ‣ "TTJTUͳͲͷແޮԽ ‣ ࣍ͷ($ىಈ༻ͷώʔϓαΠζΛࢉग़
  46. ©2019 Wantedly, Inc. 4XFFQ w લॲཧ ‣ gcphase_GCmarktermination_GCoff ‣ 8SJUFCBSSJFSΛແޮԽ

    w (P͸5$.BMMPDΛϕʔεͱͨ͠"MMPDBUJPOػߏ ‣ ίϨΫλ͕·ͩݟ͍ͯͳ͍ྖҬʹΦϒδΣΫτΛ֬อ͠ͳ͍Α ͏ʹɼ"MMPDBUJPO͢ΔલʹͦͷαΠζͷQBHFͷTXFFQॲཧΛ ਐΊ͓ͯ͘ ɾɾɾ ͜͜·ͰͷྖҬͳΒ
 നΦϒδΣΫτΛ֬อͯ͠΋େৎ෉ Heap (Page)
  47. ©2019 Wantedly, Inc. Ͱ͸ͦͷઌ͸ʁ

  48. ©2019 Wantedly, Inc. ະདྷͷ(P($ w ($ΞϧΰϦζϜɼύϥμΠϜ͸ଞʹ·ͩ·ͩ͋Δ w ੈ୅ผ($Λಋೖ͍ͨ͠ͱ͍͏࿩͸͋Δ ‣ 3JDL

    IUUQTCMPHHPMBOHPSHJTNNLFZOPUF *4.. ‣ ϨΠςϯγ͸Լ͕Δ͕8SJUFCBSSJFSʹΑͬͯɼ εϧʔϓοτ͕Լ͕ΔՄೳੑ͕͋Δ w ۩ମతͳٞ࿦͸·ͩݟ౰ͨΒͳ͍ ݟ͚ͭͯͳ͍ ͕ɼ (PͳͲͰͲ͏มΘָ͍͔͕ͬͯ͘͠Έ
  49. ©2019 Wantedly, Inc. ·ͱΊ w $PODVSSFOU.BSL4XFFQ($Λ࠾༻͍ͯ͠Δ ‣ ($ىಈத͸($༻ͷHPSPVUJOF͕࡞ಈ͍ͯ͠Δ w ($ͷىಈλΠϛϯά͸؀ڥม਺GOGCͰௐ੔Մೳ

    w ($͸੒௕͠ଓ͚͍ͯΔ ‣ 3FMFBTFOPUF΍1SPQPTBMΛಡΉͷָ͕͠ΈͰ͢Ͷ
  50. ©2019 Wantedly, Inc. ͋Γ͕ͱ͏͍͟͝·ͨ͠