Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Memory Management in Chrome's JavaScript Runtime (V8) at #code16

1,205 views

Published on

Long garbage collection pauses in JavaScript, can lead to noticeable performance degradation and poor user-experiences. Understanding the garbage collection mechanism is essential to building advanced applications with effective memory management.

This talk covers the fundamentals of automatic memory management in the V8 JavaScript engine. We also discuss some upcoming optimisations that are aimed directly at tackling garbage collection jank.

Further reading at https://hadi.io/code16

Published in: Internet
  • Be the first to comment

Memory Management in Chrome's JavaScript Runtime (V8) at #code16

  1. 1. @hadi_michael #code16 MEMORY MANAGEMENT IN CHROME’S JAVASCRIPT RUNTIME (V8)
  2. 2. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Understanding memory management in JavaScript is about more than avoiding memory leaks
  3. 3. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) JavaScript’s portability made it an attractive target for distributed machine learning
  4. 4. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Long-running computation on large data-structures, completely paused the page
  5. 5. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Let’s learn about sports
  6. 6. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Profiling with Chrome’s DevTools Plenty more tools (e.g. timeline, object allocation tracker) here: 
 https://developers.google.com/web/tools/chrome-devtools/profile/memory-problems/memory-diagnosis
  7. 7. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Learning time increased proportional to document word countTimetoLearn(s) 0 2.8 5.6 8.4 11.2 14 Document Word Count (MM words) 0 2 3 5 6 8 10 11 13 14 16
  8. 8. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Heap size was increasing proportional to document sizeHeapSize(MB) 0 50 100 150 200 250 300 Document Size (MB) 0 10 20 30 40 50 60 70 80 90
  9. 9. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The classifier’s memory footprint grew mostly with vocabulary size VocabularySize(Kwords) 0 4 8 12 16 20 24 28 32 36 40 ClassifierMemoryFootprint(MB) 0 1 2 3 4 5 6 Document Word Count (words) 162,318 469,012 1,031,151 1,459,582 7,297,910 14,595,820
  10. 10. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Heap size comparison shows growth in System Objects BEFORE AFTER .learn()
  11. 11. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The application was running GC operations frequently Filesize: 84.7MB Time: 13640ms Total number of words: 14,595,820 Vocabulary size: 37,042 words (garbage collector)
  12. 12. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 30% of processing time was spent on GC
  13. 13. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Let’s talk about memory in JavaScript
  14. 14. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Values are stored on the Heap q r •1 Heap 45 • • 6 23 • 4 • 117 • 75 Program variables
  15. 15. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Memory can be represented by a directed Object Graph r 5 4 75 23 11 4 7 6 Root Object Value Scalar Value
  16. 16. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) A value can have one or more retaining paths
  17. 17. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) A value can have one or more retaining paths
  18. 18. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) A value is removed from the graph by cutting its retaining path
  19. 19. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The compiler will guarantee that any live record is reachable from the root
  20. 20. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) “Garbage” is heap-allocated records that are not reachable by any chain of pointers
  21. 21. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) “Garbage” is all values that no longer have a retain path
  22. 22. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) “Garbage collection” is reclaiming the memory occupied by garbage for use in allocating new records
  23. 23. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) “Garbage collection” makes memory available for new allocations
  24. 24. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The more objects there are on the heap, the longer it takes to collect all the garbage
  25. 25. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The Weak Generational Hypothesis states that 
 most objects die young
  26. 26. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) V8 implements a generational collector that splits values between young and old generations
  27. 27. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The young generation uses a semi-space (stop-and-copy) scavenger algorithm
  28. 28. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger From Space To Space
  29. 29. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory To Space A Allocate A
  30. 30. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory To Space A Allocate B B
  31. 31. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory To Space A Allocate C B C
  32. 32. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory To Space A Allocate D B C D
  33. 33. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger To Space A Not enough space for E B C D E
  34. 34. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory To Space A B C D Page is paused Collection is triggered
  35. 35. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory A B C D To Space Walk the graph and copy live values
  36. 36. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory A B C D Reachable values are found and copied Unallocated memoryA C
  37. 37. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory Memory in the From space is now available A C From Space
  38. 38. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory From and To spaces swap roles A C To Space
  39. 39. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory A C To Space E Allocate E
  40. 40. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: scavenger Unallocated memory A C To Space E Candidates for promotion at next GC
  41. 41. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Young generation: key points Unallocated memory A C To Space E Split the heap into two spaces Allocation is fast Copy space is compacted Cost of a GC is proportional to the number of live objects in the graph
  42. 42. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) The old generation uses a mark-sweep-compact algorithm
  43. 43. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: heuristics govern heap availability Uncommitted restLive memory Growing Total heap Available heap
  44. 44. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: full GC is triggered A B C D E F G
  45. 45. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: mark A B | C D E F | | |
  46. 46. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: sweep A B | C D E F | | |
  47. 47. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: compact A C E F
  48. 48. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: promoting surviving records A C E F G Allocate G
  49. 49. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Old generation: key points A C E F G The mark phase takes time proportional 
 to the number of nodes it marks Uses all the committed heap The sweep and compact phases can take time proportional to the size of the heap
  50. 50. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) GC components can be made to be incremental, concurrent, and/or parallel
  51. 51. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Orinoco is based on a mostly parallel and concurrent GC, with less jank and higher throughput
  52. 52. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1. Incremental marking Orinoco adds a range of optimisations to V8 Learn more: 
 https://v8project.blogspot.com.au/2015/10/jank-busters-part-one.html
 https://v8project.blogspot.com.au/2016/04/jank-busters-part-two-orinoco.html Source: BlinkOn 5: V8 Garbage Collection
 https://www.youtube.com/watch?v=lqz_rXCoAIc
  53. 53. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1. Incremental marking 2. Concurrent sweeping Orinoco adds a range of optimisations to V8 BEFORE Learn more: 
 https://v8project.blogspot.com.au/2015/10/jank-busters-part-one.html
 https://v8project.blogspot.com.au/2016/04/jank-busters-part-two-orinoco.html
  54. 54. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1. Incremental marking 2. Concurrent sweeping Orinoco adds a range of optimisations to V8 AFTER Learn more: 
 https://v8project.blogspot.com.au/2015/10/jank-busters-part-one.html
 https://v8project.blogspot.com.au/2016/04/jank-busters-part-two-orinoco.html
  55. 55. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1. Incremental marking 2. Concurrent sweeping 3. Parallel compaction Orinoco adds a range of optimisations to V8 BEFORE Learn more: 
 https://v8project.blogspot.com.au/2015/10/jank-busters-part-one.html
 https://v8project.blogspot.com.au/2016/04/jank-busters-part-two-orinoco.html
  56. 56. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1. Incremental marking 2. Concurrent sweeping 3. Parallel compaction Orinoco adds a range of optimisations to V8 AFTER Learn more: 
 https://v8project.blogspot.com.au/2015/10/jank-busters-part-one.html
 https://v8project.blogspot.com.au/2016/04/jank-busters-part-two-orinoco.html
  57. 57. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Combined with idle-time garbage collection scheduling Learn more:
 http://research.google.com/pubs/pub45361.html Source: V8, modern JavaScript, and beyond - Google I/O 2016
 https://www.youtube.com/watch?v=N1swY14jiKc
  58. 58. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Each allocation moves you closer to a collection, collections pause your application and introduce latency. The larger the heap, the longer a collection will take.
  59. 59. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) … last we checked, we had: 44x heap growth 30% of processing time in GC 13s to complete learning
  60. 60. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Reading documents in chunks, using Streams, minimised heap growth
  61. 61. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Final heap size was down by ~96% .learn() BEFORE AFTER
  62. 62. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) 1.36x improvement on processing time spent learning .learn() is up to 94.02% from 69.36% GC is down to 1.87% from 30.62%
  63. 63. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Total time to complete improved by ~22% Filesize: 84.7MB Time: 10641ms (~22% faster) Total number of words: 14,595,820 Vocabulary size: 37,042 words
  64. 64. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Understanding memory management in JavaScript is about more than avoiding memory leaks
  65. 65. @hadi_michael | Memory Management in Chrome’s JS Runtime (V8) Understanding memory
 management in JavaScript is about boosting developer productivity
  66. 66. @hadi_michael For more, please visit: https://hadi.io/code16 THANK YOU

×