When are JavaScript objects destroyed?

In C ++, I can define the constructor and destructor explicitly, and then cout <"C or D is called" from the constructor / destructor function to know exactly where.

However, in JavaScript, how do you know when an object is destroyed. Below is an example that concerns me.

I call the internal function in a timeout, and I wonder if the object remains alive as long as the timer runs, waiting for the call again.

User Calls Control

// Calls Control 

Call Management Message

 var message_object = new Message( response_element ); 

Calling Messages Effects

 new Effects().fade( this.element, 'down', 4000 ); message_object.display( 'empty' ); 

Effects

 /** *Effects - build out as needed * element - holds the element to fade * direction - determines which way to fade the element * max_time - length of the fade */ var Effects = function( ) { this.fade = function( element, direction, max_time ) { element.elapsed = 0; clearTimeout( element.timeout_id ); function next() { element.elapsed += 10; if ( direction === 'up' ) { element.style.opacity = element.elapsed / max_time; } else if ( direction === 'down' ) { element.style.opacity = ( max_time - element.elapsed ) / max_time; } if ( element.elapsed <= max_time ) { element.timeout_id = setTimeout( next, 10 ); } } next(); } }; 
+24
javascript
Apr 11 '12 at 19:32
source share
4 answers

JS objects do not have destructors per se.

JavaScript objects (and primitives) are garbage collection when they become inaccessible , which means there is no reference to them in the current execution context. JavaScript runtime for this is constantly monitored. Therefore, if you do not use the delete keyword to delete something, then its destruction under the hood. Some browsers do not detect links that remain in the closing area (I look at you, Redmond), and therefore you often see objects that were set to zero at the end of functions to make sure memory is freed in IE.

+25
Apr 11 2018-12-12T00:
source share

This notion that destroying an object boils down to garbage collection for memory strikes me as a dangerous misrepresentation, since the problem does not boil down to freeing up memory.

Destructors are responsible for freeing other resources, such as file descriptors or event listeners, that are not automatically handled by garbage collection. In such cases, destructors are absolutely necessary to unwind the state before freeing memory or leaking resources.

In such cases, the problem is that destructors are not a first-class concept of whether they need to be explicitly named or they can be called implicitly after the object becomes inaccessible.

The best way to handle this is to properly document your modules if they need destructors to be used, and emphasize resource leakage scenarios that cannot be used in this way.

+13
May 20 '17 at 20:42
source share

ECMAscript does not have dynamic memory management. The garbage collector will take care of what requires memory in your script. Therefore, in fact, the question should be more similar,

"As the garbage collector knows, when it can free up memory for objects"

Simply put, most GCs look to see if there are any active links. This may be due to the parent context object, prototype chains, or any direct access to this object. In your particular case, setTimeout is executed at any time, it calls next() , which closes in the parent context .fade() , and the .face() function, in turn, closes the Effects function (context).

This means that as long as there are calls to setTimeout , the whole whole construct is stored in memory.

You can sometimes help older GC implementations by using null variable variables / references to them, which may collect some things sooner or in general, but modern implementations are pretty smart in that regard. In fact, you don’t have to worry about things like Live Object / Link.

+4
Apr 11 '12 at 19:40
source share

There is an experimental Firefox and Chrome window.requestIdleCallback () function that calls the browser in standby mode. This can be used to simulate a class instance destructor.

Almost the same effect can be obtained from the following code:

 setTimeout(function() { // Simulate destructor here },0); 

This sets an automatically rejected timeout, which ends when the current JavaScript script completes.

0
Jan 23 '19 at 15:15
source share



All Articles