
- 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
Explain in detail about Reference-counting garbage collection in JavaScript?
Reference-counting garbage collection
This is the simplest garbage collection algorithm.This algorithm looks out for those objects which have no references left.An object becomes eligible for garbage collection if it has no references attached to it.The garbage collection is explained in the below example.
Example
var obj = { x: { y: 2 } }; // 2 objects created. One is referenced by the other as one of its properties. // Obviously, none can be garbage-collected obj = 1; // what was the 'x' property of the object originally in obj // has zero references to it. It can be garbage collected.
Limitations
When it comes to cycles there are limitations in Reference-counting garbage collection and it is explained in the below example.
Example
In the following example,two objects were created and referenced one another there by creating a cycle. After a function call they will go out of scope, so they are effectively useless and could be freed.But the reference-counting algorithm considers that since each of the two objects is referenced at least once, neither can be garbage-collected.
function f() { var obj1 = {}; var obj2 = {}; obj1.p = obj2; // o1 references o2 obj2.p = obj1; // o2 references o1. This creates a cycle. } f();
- Related Articles
- Explain in detail about memory leaks in JavaScript?
- Explain about add and assignment(+=) operator in detail in javascript?
- Explain in detail about Mark and Sweep algorithm in JavaScript?
- Garbage collection(GC) in JavaScript?
- Explain in detail about the memory life cycle of JavaScript?
- Explain about logical not(!) operator in detail with example in javascript?
- What is JavaScript garbage collection?
- Garbage collection in Java
- Garbage Collection in Python
- Java Garbage collection
- What is garbage collection in C#?
- Destroying Objects (Garbage Collection) in Python
- Destructors and Garbage Collection in Perl
- Explain Force in detail
- Explain sound in detail.

Advertisements