Share
Embed
JavaScript Essentials: Speicherverwaltung (German)
Speicherverwaltung in Javascript. Gibt es hierzu eigentlich viel zu sagen? Im Gegensatz zu unseren C++-Kollegen, müssen wir das Speichermanagement in unserer Anwendung nicht selbst entwickeln. Denn das JavaScript-Speichermanagement ist automatisiert und läuft im Hintergrund. Trotzdem müssen wir zumindest die grundlegenden Prinzipien verstehen, damit unser Code keine Speicherlecks aufweist.
In diesem Vortrag werden die Grundlagen der Speicherverwaltung behandelt. Danach sehen wir uns die Möglichkeiten der Chrome DevTools und bevor wir uns mit den möglichen Problemfällen befassen, gibt es einen Ausflug in die prinzipielle Funktionsweise eines Garbage Collectors.
Rainer Hahnekamp
June 26, 2019
Transcript
49 slides
-
Teil I: Allgemeine Theorie Speicherverwaltung Allgemeine Theorie Analyse Probleme Memory
Leaks Garbage Collector -
Betriebssystem Prozess JavaScript Engine JavaScript Quelltext const product = {
name: "Äpfel", category: "Früchte" }; Virtueller Speicher Speicherhierarchie -
Betriebssystem Prozess JavaScript Engine JavaScript Quelltext const product = {
name: "Äpfel", category: "Früchte" }; Virtueller Speicher Stack Heap Speicherhierarchie -
Betriebssystem Prozess JavaScript Engine JavaScript Quelltext const product = {
name: "Äpfel", category: "Früchte" }; Virtueller Speicher Heap Stack Speicherhierarchie Garbage Collector -
Heap Größen von Websites Website HEAP Google Suche 7 Amazon.de
21 Twitter 42 Facebook 43 Slack 62 Google Slides 108 GMail 115 Google Maps (3D) 144 -
Teil III: Garbage Collector Speicherverwaltung Allgemeine Theorie Analyse Probleme Memory
Leaks Garbage Collector -
Speicherallokation const doShopping = async () => { const basket
= [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ];total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; doShopping().then(console.log); -
Speicherallokation const doShopping = async () => { const basket
= [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") };n total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; doShopping().then(console.log); -
Speicherallokation const doShopping = async () => { const basket
= [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; doShopping().then(console.log); -
Speicherallokation const doShopping = async () => { const basket
= [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; -
Speicherallokation const doShopping = async () => { const basket
= [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; Heap -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies total -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); i=0 total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies total product (0) -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); i=1 total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies total product (0) product (1) -
Speicherallokation Heap basket const doShopping = async () => {
const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); i=2 total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies total product (0) product (1) Garbage Collection startet... -
Mark & Sweep Heap basket const doShopping = async ()
=> { const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 } ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); i=2 total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) { const currencies = { de: "€", gb: "£", cn: "¥" }; return currencies[country]; }; currencies total product (0) product (1) Garbage Collection starting... -
basket total product (1) product (0) Engine from-space to-space Mark
& Sweep: Sweeping currencies -
basket total product (1) product (0) Engine from-space to-space Mark
& Sweep: Sweeping currencies basket total -
product (1) product (0) Engine from-space to-space Mark & Sweep:
Sweeping currencies basket total -
Infant Mortality Die meisten Objekten existieren nur eine kurze Zeit.
Es wäre ineffizient, diese einzeln zu entfernen. -
Und es gibt noch mehr... const doShopping = async ()
=> { const basket = [ { product: "apple", amount: 3 }, { product: "water", amount: 1 }, { product: "milk", amount: 3 }, ··· ]; const total = { price: 0, currency: getCurrency("de") }; for (let i = 0; i < basket.length; i++) { const product = await fetchProduct(basket[i].product); total.price += product.price * basket[i].amount; } return total; }; const getCurrency = function(country) {··· }; mehrfache GCs noch mehr Produkte -
Generationen Engine product (2) product (3) 1. Generation: Nursery 2.
Generation: Intermediate basket total -
Reale Garbage Collectors • Variationen bzw. verbesserte Versionen: Das Prinzip
ist jedoch dasselbe • Sehr viel Arbeit wird in schneller GC-Zyklen gesteckt. • Zusammenspiel mit der Rendering Engine des Browsers. • Anzahl der Objekte im Heap viel, viel höher. -
Teil IV: Probleme - Memory Leaks Speicherverwaltung Allgemeine Theorie Analyse
Probleme Memory Leaks Garbage Collector -
Globaler Scope 1/3 - kein var, let, const (function() {
storage = new Array(1000000).fill("x"); })(); -
Globaler Scope 2/3 - Typos (function() { let storage =
""; console.log("current length: " + storage.length); storge = new Array(1000000).fill("x"); })(); -
Globaler Scope 3/3 - Falscher Context (function() { const person
= { introduce: function() { console.log(this.introduction); }, setIntroduction: function(introduction) { this.introduction = introduction; } }; const getData = () => new Promise(resolve => resolve(new Array(1000000).fill("x"))); getData().then(person.setIntroduction); person.introduce(); })(); -
DOM Nodes - losgelöste Elemente const ul = document.createElement("ul"); run5Times("detached",
() => { for (let i = 0; i < 1000; i++) { ul.appendChild(document.createElement("li")); } }); -
getPromotionFn getName printName data name async function getPromotionFn() { const
data = await loadPromotion(); const getName = function() { return data[0]; }; const name = getName(); return function printName() { alert(`Kaufen Sie ${name}!!!`); }; } sollte im Speicher bleiben sollte entfernt werden (GC'ed) -
getPromotionFn Verschachtelte Funktionen teilen sich denselben Scope Scope getName printName
data name data name async function getPromotionFn() { const data = await loadPromotion(); const getName = function() { return data[0]; }; const name = getName(); return function printName() { alert(`Kaufen Sie ${name}!!!`); }; } -
getPromotionFn Scope getName printName data name name async function getPromotionFn()
{ const data = await loadPromotion(); const getName = function() { return data[0]; }; const name = getName(); return function printName() { alert(`Kaufen Sie ${name}!!!`); }; } -
Zusammenfassung • Heap als Haupt-Speicherstruktur in JavaScript • Google DevTools
ermöglichen ◦ Heap Dumps (statisch Analyse) ◦ Verwendung des Heaps über Zeitraum zu messen (dynamische Analyse) • Garbage Collectoren verwenden sogenannte Roots um zu bestimmen, ob ein Objekt de-allokiert werden kann • Garbage Collections laufen als "Batch" • Closure können schwer auffindbare Memory Leaks verursachen