├── docs ├── JQuery │ ├── README.md │ └── PrevObject.md ├── Callbacks │ ├── README.md │ └── EndpointStatus.md ├── DOMReferences │ ├── README.md │ └── SimpleStaticMemoryLeak.md ├── NotKilledTimers │ ├── README.md │ └── GonzaloRuizDeVillaModifiedExample.md ├── GlobalVariables │ ├── README.md │ └── CacheService.md └── Closures │ ├── README.md │ ├── MeteorJSExample.md │ ├── NotUsedCode.md │ └── Promise.race.md └── README.md /docs/JQuery/README.md: -------------------------------------------------------------------------------- 1 | ## jQuery 2 | 3 | * [prevObject](./PrevObject.md) 4 | -------------------------------------------------------------------------------- /docs/Callbacks/README.md: -------------------------------------------------------------------------------- 1 | ## Callbacks 2 | 3 | * [Endpoint status](./EndpointStatus.md) 4 | -------------------------------------------------------------------------------- /docs/DOMReferences/README.md: -------------------------------------------------------------------------------- 1 | ## DOM References 2 | 3 | * [Simple static memory leak](./SimpleStaticMemoryLeak.md) 4 | -------------------------------------------------------------------------------- /docs/NotKilledTimers/README.md: -------------------------------------------------------------------------------- 1 | ## Not killed timers 2 | 3 | * [Gonzalo Ruiz de Villa Modified Example](./GonzaloRuizDeVillaModifiedExample.md) 4 | -------------------------------------------------------------------------------- /docs/GlobalVariables/README.md: -------------------------------------------------------------------------------- 1 | ## Global variables 2 | 3 | Scripts create objects into global scope: 4 | 5 | * [Cache service](./CacheService.md) 6 | -------------------------------------------------------------------------------- /docs/Closures/README.md: -------------------------------------------------------------------------------- 1 | ## Closures 2 | 3 | * [Not used code](./NotUsedCode.md) 4 | * [MeteorJS Example](./MeteorJSExample.md) 5 | * [Promise.race Example](./Promise.race.md) 6 | -------------------------------------------------------------------------------- /docs/Closures/MeteorJSExample.md: -------------------------------------------------------------------------------- 1 | ## MeteorJS Example 2 | 3 | [Source of example](https://blog.meteor.com/an-interesting-kind-of-javascript-memory-leak-8b47d2e7f156) 4 | 5 | ```js 6 | var theThing = null 7 | var replaceThing = function () { 8 | var originalThing = theThing 9 | var unused = function () { 10 | if (originalThing) 11 | console.log("hi") 12 | } 13 | theThing = { 14 | longStr: new Array(1000000).join('*'), 15 | someMethod: function () { 16 | console.log(someMessage) 17 | } 18 | } 19 | } 20 | setInterval(replaceThing, 1000) 21 | ``` 22 | 23 | **How to fix** 24 | 25 | ```js 26 | 27 | var theThing = null 28 | var replaceThing = function () { 29 | var originalThing = theThing 30 | var unused = function () { 31 | if (originalThing) 32 | console.log("hi") 33 | } 34 | theThing = { 35 | longStr: new Array(1000000).join('*'), 36 | someMethod: function () { 37 | console.log(someMessage) 38 | } 39 | } 40 | originalThing = null; 41 | } 42 | setInterval(replaceThing, 1000) 43 | ``` 44 | 45 | -------------------------------------------------------------------------------- /docs/Closures/NotUsedCode.md: -------------------------------------------------------------------------------- 1 | ## Not used code 2 | 3 | [Source of example](https://bugs.chromium.org/p/chromium/issues/detail?id=315190) 4 | 5 | Memory can't be released because it can't be allocated before 6 | 7 | ```js 8 | function f() { 9 | var some = []; 10 | while(some.length < 1e6) { 11 | some.push(some.length); 12 | } 13 | function unused() { some; } //causes massive memory leak 14 | return function() {}; 15 | } 16 | 17 | var a = []; 18 | var interval = setInterval(function() { 19 | var len = a.push(f()); 20 | document.getElementById('count').innerHTML = len.toString(); 21 | if (len >= 500) { 22 | clearInterval(interval); 23 | } 24 | }, 10); 25 | ``` 26 | 27 | **How to fix:** investigate and refactor code, try to remove not used functional 28 | 29 | ```js 30 | function f() { 31 | return function() {}; 32 | } 33 | 34 | var a = []; 35 | var interval = setInterval(function() { 36 | var len = a.push(f()); 37 | document.getElementById('count').innerHTML = len.toString(); 38 | if (len >= 500) { 39 | clearInterval(interval); 40 | } 41 | }, 10); 42 | ``` 43 | -------------------------------------------------------------------------------- /docs/GlobalVariables/CacheService.md: -------------------------------------------------------------------------------- 1 | ## Cache service 2 | 3 | **What is a memory leak:** after removing `CacheService` instance memory must be released 4 | 5 | ```js 6 | (function(window) { 7 | cache = [] 8 | 9 | window.CacheService = function() { 10 | return { 11 | cache: function(value) { 12 | cache.push(new Array(1000).join('*')) 13 | cache.push(value) 14 | } 15 | } 16 | } 17 | })(window) 18 | 19 | var service = new window.CacheService() 20 | 21 | for (let i=0; i < 99999; i++) { 22 | service.cache(i) 23 | } 24 | 25 | service = null 26 | ``` 27 | 28 | **How to fix:** move `cache` variable to `CacheService` scope like the followings: 29 | 30 | ```js 31 | (function(window) { 32 | window.CacheService = function() { 33 | var cache = [] 34 | 35 | return { 36 | cache: function(value) { 37 | cache.push(new Array(1000).join('*')) 38 | cache.push(value) 39 | } 40 | } 41 | } 42 | })(window) 43 | 44 | var service = new window.CacheService() 45 | 46 | for (let i=0; i < 99999; i++) { 47 | service.cache(i) 48 | } 49 | 50 | service = null 51 | ``` 52 | -------------------------------------------------------------------------------- /docs/DOMReferences/SimpleStaticMemoryLeak.md: -------------------------------------------------------------------------------- 1 | ## Simple static memory leak 2 | 3 | ```html 4 |