forked from warsawjs/meetup-slides-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
315 lines (258 loc) · 11.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html lang="en">
<head>
<title>WarsawJS Meetup: Debugging memory leaks</title>
<meta charset="utf-8"/>
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
<meta name="viewport" content="width=device-width"/>
<link href="images/icons/warsawjs-logo-transparent-32x32.png" rel="icon" type="image/png" sizes="32x32"/>
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:400,900&subset=latin-ext" rel="stylesheet"/>
<!-- Shower Theme -->
<link rel="stylesheet" href="vendors/shower-ribbon/styles/screen-16x9.css"/>
<link rel="stylesheet" href="vendors/shower-warsawjs/styles/main.css"/>
<!-- Prism.js -->
<link rel="stylesheet" href="vendors/prism/prism.css"/>
<link rel="stylesheet" href="vendors/prism/custom-prism.css"/>
<!-- my additions -->
<link rel="stylesheet" href="styles/my-theme.css">
</head>
<body class="shower list">
<header class="caption">
<h1>WarsawJS Meetup: Debugging memory leaks</h1>
<p>We talk about JavaScript. Each month in Warsaw, Poland.</p>
</header>
<section class="slide front-page">
<div class="logo">
<!-- Logo WarsawJS -->
<img src="vendors/shower-warsawjs/images/logo.svg" width="350" alt=""/>
</div>
<div class="details">
<!-- 1. Avatar -->
<img src="images/mg.png" alt="Speaker" width="200"/>
<!-- 2. Speaker -->
<h2>Maciej Gołaszewski</h2>
<!-- 3. Presentation title -->
<h2><strong>"WarsawJS Meetup: Debugging memory leaks" [EN]</strong></h2>
<!-- 4. Presentation date -->
<h2>2019-12-12</h2>
<!-- 5. Contact the speaker -->
<h2><a href="http://twitter.com/jodator">@jodator</a></h2>
</div>
</section>
<section class="slide compact">
<h2>Memory management in JavaScript</h2>
<div class="double">
<p>JavaScript automatically allocates memory when objects are created.</p>
<p><strong>Garbage collection</strong> - is a process of automatically freeing memory when those objects are no longer used.</p>
<p class="next">Memory life-cycle:</p>
<ol>
<li class="next">allocate</li>
<li class="next">use</li>
<li class="next">release</li>
</ol>
</div>
</section>
<section class="slide compact">
<h2>Step 1: Allocate</h2>
<ul>
<li>Initial declaration of primitives (like <code>Number</code> or <code>String</code>).</li>
<li>For <code>Object</code> & <code>Array</code> - it also allocates memory for contained values.</li>
<li>Function declaration (<code>() => {}</code>) allocates a callable object (<code>Function</code>).</li>
<li>Object creation <code>new Foo()</code>, <code>new Date()</code>, etc.</li>
<li>Element creation - <code>document.createElement( 'div' )</code>.</li>
<li>Array and string manuipulation methods: <code>array.concat( otherArray )</code>, <code>'JavaScript'.substr( 0, 4 )</code>,
...
</li>
</ul>
</section>
<section class="slide compact">
<h2>Step 2: Use</h2>
<figure class="place bottom">
<img src="images/gifs/source.gif" width="450">
<figcaption class="white">Source <a href="https://giphy.com/gifs/cat-pizza-lazers-zdzG4ByVrd1V6?utm_source=media-link&utm_medium=landing&utm_campaign=Media%20Links&utm_term=">giphy</a>.</figcaption>
</figure>
</section>
<section class="slide prism">
<h2>Step 3: Release</h2>
<div class="double">
<p>The <i>Garbage collector</i> finds the memory which is no longer need.</p>
<p>All modern browsers uses the "mark-and-sweep" algorithm to detect objects that are no longer needed by finding "unreachable objects".</p>
<figure>
<img src="images/dontcontrol.png" width="450">
<figcaption class="white reference">Source <a
href="https://developers.google.com/web/tools/chrome-devtools/memory-problems/memory-101">Google Tools for Web
Developers: Memory Terminology</a>.
</figcaption>
</figure>
</div>
<blockquote>
<p>As of 2019 We cannot release memory manually (call the garbage collector).</p>
</blockquote>
</section>
<section class="slide compact">
<h2>Common causes of memory leaks</h2>
<ul>
<li class="next">Reference in global objects.</li>
<li class="next">Forgotten event listeners & callbacks.</li>
<li class="next">Referenced, detached DOM nodes.</li>
<li class="next">Variables and closures in shared lexical scope.</li>
</ul>
<p class="next">But only if those are not needed by the application at given time. Otherwise those are valid objects.</p>
</section>
<section class="slide compact">
<h2>Available Tools</h2>
<ul>
<li>Google Chrome dev tools - memory tab.</li>
<li>Firefox dev tool - memory tab.</li>
<li>Use <code>window.performance.memory</code> to programmatically read used memory.</li>
</ul>
<figure>
<img src="images/window_preformance_memory.png" width="572" alt="">
</figure>
</section>
<section class="slide prism">
<h2>Using the tools - preparation</h2>
<p>Run your browser with all features striped down.</p>
<pre>
<code class="language-javascript line-numbers">
google-chrome \
--disable-extensions \
--disable-plugins \
--incognito
http://example.com
</code>
</pre>
</section>
<section class="slide compact">
<h2>Firefox dev tools</h2>
<figure class="place">
<img src="images/ff_dev_tools.png" width="800" alt="">
</figure>
<figure class="place next">
<img src="images/ff_dev_tools_better.png" width="800" alt="">
</figure>
</section>
<section class="slide compact">
<h2>Chrome dev tools</h2>
<figure>
<img src="images/chrome_dev_tools.png" width="850" alt="">
</figure>
</section>
<section class="slide compact">
<h2>Chrome dev tools - allocation timeline</h2>
<figure>
<img src="images/debug_timeline.png" width="850" alt="">
</figure>
</section>
<section class="slide compact">
<h2>Chrome dev tools - comparing snapshots</h2>
<figure>
<img src="images/debug_compare.png" width="850" alt="">
</figure>
</section>
<section class="slide compact">
<h2>Live debugging</h2>
<figure class="place bottom">
<img src="images/gifs/debug.gif" width="400">
<figcaption class="white">Source <a href="https://giphy.com/gifs/6gTbmSzezq5Dq/media">giphy</a>.</figcaption>
</figure>
</section>
<section class="slide prism">
<h3>Example: Accidental global (<a href="demo.html" target="_blank" rel="noopener noreferrer">Demo</a>)</h3>
<pre style="font-size: 30px;" class="language-javascript line-numbers">
<code>
document.getElementById( 'ml-global' )
.addEventListener( 'click', () => {
// window.bo = new BigObject();
// bo = new BigObject();
this.bo = new BigObject(); // this === window
} );
</code>
</pre>
</section>
<section class="slide prism">
<h3>Example: Intermediate values (<a href="demo.html" target="_blank" rel="noopener noreferrer">Demo</a>)</h3>
<pre style="font-size: 30px;" class="language-javascript line-numbers">
<code style="line-height: 1.5em;">
const config = { bo: new BigObject(), foo: 'bar',
interval: 1000 };
logSomething( config );
function logSomething( config ) {
setInterval( () => { // Also: calback!
console.log( config.foo );
}, config.interval );
}
</code>
</pre>
</section>
<section class="slide prism">
<h3>Example: Shared lexical scope (<a href="demo.html" target="_blank" rel="noopener noreferrer">Demo</a>) <span class="reference"> from <a
href="https://blog.meteor.com/an-interesting-kind-of-javascript-memory-leak-8b47d2e7f156"
target="_blank" rel="noopener noreferrer">An interesting kind of JavaScript memory leak</a>.</span></h3>
<pre style="font-size: 24px;" class="language-javascript line-numbers">
<code style="line-height: 1.5em;">
let theThing = null;
function replaceThing() {
const originalThing = theThing;
function unused() {
if ( originalThing ) {
// ...
}
}
theThing = {
bo: new BigObject(),
someMethod: function() {
// ...
}
};
}
</code>
</pre>
</section>
<section class="slide prism">
<h3>Example: Forgotten stuff (<a href="demo.html" target="_blank" rel="noopener noreferrer">Demo</a>)</h3>
<pre style="font-size: 24px;" class="language-javascript line-numbers">
<code style="line-height: 1.6em;">
const bo = new BigObject();
bo.node = document.getElementById( 'a-div' )
function detachNode() {
document.getElementById( 'a-div' ).remove(); // Removes from the DOM
}
setInterval( () => { // Interval is not cleared
console.log( bo[ 3 ] );
}, 500 );
console.log( bo ); // bo resides in console GC root
</code>
</pre>
</section>
<section class="slide compact">
<h2>ProTips™</h2>
<ul>
<li>Be careful with anonymous functions for callbacks (if the scope is shared).</li>
<li>Pass only what you need to functions.</li>
<li>De-register callbacks (use named functions).</li>
<li>Consider using <code>WeakMap</code> or <code>WeakSet</code>.</li>
<li>GC must pause execution to collect memory.</li>
<li>Measure, when testing - disable all addons.</li>
<li class="next">Hidden garbage collector: <code>google-chrome -js-flags="--expose-gc"</code> - <code>window.gc()</code>.</li>
</ul>
</section>
<section class="slide">
<h3 class="shout">Thanks!</h3>
</section>
<div class="progress"></div>
<footer class="badge">
<a href="https://github.com/jodator/warsawjs-debugging-memory-leaks">Fork me on GitHub</a>
</footer>
<footer class="badge badge-top-left">
<a href="#" class="fullscreen">Fullscreen</a>
</footer>
<script src="vendors/gamepad/gamepad.js"></script>
<script src="vendors/shower/shower.min.js"></script>
<script src="vendors/shower-gamepad/shower.gamepad.js"></script>
<script src="vendors/shower-warsawjs/scripts/fullscreen.js"></script>
<!-- Prism.js -->
<script src="vendors/prism/prism.js"></script>
<script src="vendors/prism/custom-prism.js"></script>
</body>
</html>