
- 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 allocate 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 to allocate memory to an object whose length is set to 0 - JavaScript?
- How to free up the memory in JavaScript?
- How to clear cache memory using JavaScript?
- How to dynamically allocate a 2D array in C?
- How are variables allocated memory in JavaScript?
- Memory Management in JavaScript
- Eradicating Memory Leaks in Javascript
- IntBuffer allocate() method in Java
- FloatBuffer allocate() method in Java
- DoubleBuffer allocate() method in Java
- ShortBuffer allocate() method in Java
- ByteBuffer allocate() method in Java
- CharBuffer allocate() method in Java
- How can circular references cause memory leakage in JavaScript?
- How can Detached DOM elements cause memory leak in JavaScript?

Advertisements