Server Memory - Chernivtsi JS 2019

Server Memory - Chernivtsi JS 2019

Transcript

  1. None
  2. @listochkin fb.com/tektonna

  3. Not a JavaScript talk

  4. No CSS No frameworks No Node

  5. A bit deeper than most talks on similar subject

  6. None
  7. How programming languages work

  8. Just-in-time compilers Garbage Collection

  9. Memory Management

  10. Server runs our programs

  11. CPU Caches RAM Swap Disk Network

  12. Operating System

  13. None
  14. Pages

  15. 4k

  16. Virtual

  17. 2 processes [0 … ∞]

  18. Oh, I got a pointer at 4096 What’s at 4095?

  19. Page fault

  20. Allocate page But not really

  21. Once you start writing to it

  22. not 4k?

  23. None
  24. None
  25. malloc

  26. None
  27. malloc me some memory

  28. a pointer

  29. free(pointer)

  30. malloc malloc malloc free malloc free free

  31. Forget to free Free the wrong pointer Read from unallocated

    memory
  32. Can we do better?

  33. Reference Counting

  34. None
  35. const players = [ … ] players.forEach(p => makeMove(p))

  36. a = … doX(a) yield a

  37. scope

  38. if ( … ) { let a = … …

    }
  39. None
  40. C++: std::shared_ptr

  41. None
  42. Optimizations

  43. Delayed Counting

  44. Don’t count local references

  45. function (user) { let email = user.email; … … }

  46. Delayed deallocation

  47. Cycle Collector

  48. None
  49. None
  50. Compile time ref-counting

  51. Several types of pointers

  52. Ownership

  53. Forbids cycles by forbidding several owning references at the same

    time
  54. None
  55. None
  56. None
  57. Perl 1987 - today Python 1991 - 2001 PHP 1994

    - 2009
  58. Multiprocess deploys

  59. fork

  60. Copy on Write

  61. Worker Accepts requests As memory use raises: Stop accepting requests

    Complete in-flight requests Terminate
  62. Master Keep track on workers Start new workers Signal them

    to terminate when memory pressure is high
  63. “Pre-fork”

  64. Load the framework Load app code Run full GC Start

    forking process to accept requests
  65. None
  66. None
  67. “Processes”

  68. Tracing GC

  69. 1959 60 years ago

  70. Start at Root references Follow all references Build a live

    objects tree Delete all objects not part of the tree
  71. Ruby Java JavaScript Lua Go

  72. Roots?

  73. Constants Global variables Local variables Closures Thread-locals ...

  74. Mark & Sweep vs Mark-Compact

  75. None
  76. Can you move objects after mark?

  77. Team Sweep: Go Ruby* Lua Embedded JS engines Erlang

  78. Pros: Pointers don’t change Native extensions Easier to implement

  79. Team Compact: Java JavaScript Ruby* Haskell

  80. Pros: Less memory fragmentation over time Cons: Harder Takes longer

    to do a GC
  81. Mark all memory

  82. None
  83. Incremental Marking

  84. 3-colored algorithm by Dijkstra™

  85. None
  86. None
  87. None
  88. None
  89. None
  90. What is a barrier? if statement

  91. else branch is very rare CPU branch predictor

  92. Parallel marking

  93. None
  94. None
  95. Lazy Sweep

  96. Generational

  97. Temporary data

  98. Major vs Minor GC

  99. Pointers from Old objects to new objects

  100. Remembered Set

  101. Major GC Scan roots only Bigger object graph

  102. Minor GC Scan roots + RS Small object graph

  103. None
  104. Modern GCs are hybrid

  105. None
  106. None
  107. C-extensions

  108. Can’t move objects if their references are passed to an

    extension
  109. Can’t add WB

  110. RGen GC

  111. 2 types of objects WB-protected WB-unprotected

  112. WBu are never OldGen

  113. OldGen -> WBu WBu to Remembered Set

  114. Mark WBus on every minor GC

  115. 1 stw to mark all WBu in RS

  116. None
  117. None
  118. Adding compaction for WBp

  119. None
  120. V8

  121. Minor GC Parallel

  122. Major Parallel marking Parallel / Concurrent Compact || Sweep

  123. None
  124. When to trigger GC?

  125. Out-of-Bounds GC

  126. request? Minor GCs response is sent? Major GC

  127. Firefox Run GC in background tabs first instead of current

    tab
  128. Chrome Animation frame

  129. Walking the memory

  130. Cache locality

  131. GPU

  132. OS Pages pre-forked processes malloc zones GC Pages Remembered Sets

    Barriers
  133. None
  134. None