Server Memory - BuildStuff Ukraine 2019

Server Memory - BuildStuff Ukraine 2019

Transcript

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

  3. Memory Management

  4. A bit deeper than most talks on similar subject

  5. None
  6. CPU Caches RAM Swap Disk Network

  7. Operating System

  8. None
  9. Pages 4k

  10. Virtual

  11. 3 processes [0 … ∞] [0 … ∞] [0 …

    ∞]
  12. Allocate page But not really

  13. None
  14. malloc calloc realloc

  15. None
  16. pointer = malloc(size)

  17. free(pointer)

  18. malloc malloc malloc free malloc free free

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

    memory
  20. Can we do better?

  21. Reference Counting

  22. None
  23. const players = [ … ] players.forEach(p => makeMove(p))

  24. a = … doX(a) yield a

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

    }
  26. C++: std::shared_ptr

  27. None
  28. Optimizations

  29. Delayed Counting

  30. Don’t count local references

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

  32. Delayed deallocation

  33. Reference Cycles

  34. None
  35. None
  36. Compile time ref-counting

  37. Several types of pointers

  38. Ownership

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

    time
  40. None
  41. None
  42. None
  43. Perl 1987 - today Python 1991 - 2001 PHP 1994

    - 2009
  44. Multiprocess deploys

  45. fork

  46. Copy on Write

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

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

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

  50. Load the framework Load app code Run full GC ----

    Start forking process to accept requests
  51. None
  52. None
  53. “Processes” Supervision Trees

  54. Tracing GC

  55. 1959 60 years ago

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

    objects tree Delete all objects not part of the tree
  57. Ruby Java JavaScript Lua Go Default Choice

  58. Roots?

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

  60. Mark & Sweep vs Mark-Compact

  61. None
  62. Team Sweep: Go Ruby* Lua Embedded JS engines Erlang

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

  64. Team Compact: Java JavaScript Ruby* Haskell

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

    to do a GC
  66. Incremental Marking

  67. 3-colored algorithm by Dijkstra™

  68. None
  69. None
  70. None
  71. None
  72. None
  73. What is a barrier? if statement

  74. CPU branch predictor

  75. Parallel marking

  76. None
  77. Lazy Sweep

  78. Generational

  79. Temporary data data.filter( … ).map( … ) log(`… ${data}`) JSON.stringify(result)

  80. Major vs Minor GC

  81. Pointers from Old objects to new objects

  82. Remembered Set

  83. Major GC Scan roots only Bigger object graph

  84. Minor GC Scan roots + RS Small object graph

  85. None
  86. Modern GCs are hybrid

  87. None
  88. None
  89. C-extensions

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

    extension
  91. Can’t add WB

  92. RGen GC

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

  94. WBu are never OldGen

  95. OldGen -> WBu WBu to Remembered Set

  96. Mark WBus on every minor GC

  97. Adding compaction for WBp

  98. None
  99. V8

  100. Minor GC Parallel

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

  102. None
  103. When to trigger GC?

  104. Out-of-Bounds GC

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

    tab
  106. Chrome Animation frame

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

    Barriers
  108. None
  109. None
  110. Practical Advice

  111. Memory Leaks Multiprocess Deploys

  112. Throughput ⇔ Latency

  113. Throughput Batch jobs Queues

  114. Latency Requests Messaging

  115. Keep different types of workloads in different processes

  116. More memory Swap with SSDs

  117. Experiment and measure

  118. @listochkin Hire Me!