
- 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 to free up the memory in JavaScript?
Regardless of the programming language, memory life cycle is pretty much always the same −
- Allocate the memory you need
- Use the allocated memory (read, write)
- Release the allocated memory when it is not needed anymore
The second part is explicit in all languages. Use of allocated memory needs to be done by the developer.
The first and last parts are explicit in low-level languages like C but are mostly implicit in high-level languages like JavaScript.
Hence there is no explicit way to allocate or free up memory in JavaScript. Just initializing objects allocates memory for them. When the variable goes out of scope, it is automatically garbage collected(frees up memory taken by that object.)
Example
function test() { // Allocate and use the memory let x = { name: "John", age: 24 } console.log(x); return null; // As soon as the method goes out of scope, it is garbage collected // and it's memory freed. } test();
The cycle mentioned in the comments is carried out every time the method is called.
- Related Articles
- How does free() know the size of memory to be deallocated?
- How to get free size of internal/external memory in Android App?
- How to allocate memory in Javascript?
- Display the amount of free memory in the Java Virtual Machine
- How to get free size of internal/external memory in Android App using Kotlin?
- How to clear cache memory using JavaScript?
- How to round up to the nearest N in JavaScript
- How are variables allocated memory in JavaScript?
- Memory Management in JavaScript
- How to round up a number in JavaScript?
- Eradicating Memory Leaks in Javascript
- How can circular references cause memory leakage in JavaScript?
- How to obtain the free RAM in Arduino?
- How to uncurry a function up to depth n in JavaScript?
- How can Detached DOM elements cause memory leak in JavaScript?

Advertisements