
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can Forgotten timers or callbacks cause memory leaks in JavaScript?
Forgotten timers/callbacks
There are two timing events in javascript namely setTimeout() and setInterval(). The former executes a function after waiting a specified number of milliseconds, whereas the latter executes a function periodically(repeats for every certain interval of time).
When any object is tied to a timer callback, it will not be released until the timeout happens. In this scenario timer resets itself and runs forever till the completion of timeout there by disallowing garbage collector to remove the memory.These timers are most frequent cause of memory leaks in javascript.
Example
In the following example, the timer callback and its tied object(tiedObject) will not be released until the timeout finishes. In the mean time the timer resets itself and runs forever and therefore its memory space will never be collected even there is no reference to the original object.
<html> <body> <script> for (var i = 0; i < 100000; i++) { var tiedObject = { callAgain: function() { var text = this; var value = setTimeout(function() { text.callAgain(); }, 100000); } } tiedObject.callAgain(); tiedObject = null; } </script> </body>> </html>
Avoiding memory leaks
1.To avoid leakage provide references inside setInterval()/setTimeout() such that functions are needed to be executed before they can be garbage collected.
2.Make a direct call to remove functions once they are no longer needed.
Except for old browsers such as IE, most of the modern browsers such as chrome etc. will not face these kind of problems. More importantly libraries like jquery handle internally to make sure that no leakage problems be produced.
- Related Articles
- Eradicating Memory Leaks in Javascript
- How can circular references cause memory leakage in JavaScript?
- Explain in detail about memory leaks in JavaScript?
- How can Detached DOM elements cause memory leak in JavaScript?
- Memory leaks in Java
- How can closures cause memory leak and how to prevent it?
- JavaScript Callbacks
- Android Tools and Methods to find Memory Leaks
- How Blockchain can help prevent data leaks in the future
- Explain the working of timers in JavaScript
- Node.js – Timers Module – Cancelling Timers
- Node.js – Timers Module – Scheduling Timers
- Timers in Arduino
- How to allocate memory in Javascript?
- Callbacks in C
