Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy a JavaScript object?

Recently, I came across one of my application which consumes too much memory and increasing by 10 MB/sec.

So, I like to know how to destroy JavaScript object and variables so memory consumption stays down and my FF can't get destroyed.

I am calling two of my scripts every 8 seconds without reloading the page.

function refresh() {     $('#table_info').remove();     $('#table').hide();     if (refreshTimer) {         clearTimeout(refreshTimer);         refreshTimer = null ;     }     document.getElementById('refresh_topology').disabled=true;      $('<div id="preload_xml"></div>').html('<img src="pic/dataload.gif" alt="loading data" /><h3>Loading Data...</h3>').prependTo($("#td_123"));     $("#topo").hide();     $('#root').remove();     show_topology(); } 

How can I see which variable cause Memory overhead, what's the method to stop the execution of that process?

like image 816
Amit Shah Avatar asked Apr 20 '12 12:04

Amit Shah


People also ask

Can you delete an object in JavaScript?

Delete object propertiesThe only way to fully remove the properties of an object in JavaScript is by using delete operator. If the property which you're trying to delete doesn't exist, delete won't have any effect and can return true.

How do you destroy an instance of an object?

instance = new Class(); //since 'storage. instance' is your only reference to the object, whenever you wanted to destroy do this: storage. instance = null; // OR delete storage.

What is destroy function in JavaScript?

To "destroy" a function in javascript, simply ensure that the function becomes unreachable. This will enable the function to be eligible for reclamation.

What is JavaScript destructor?

The object destructuring is a useful JavaScript feature to extract properties from objects and bind them to variables. What's better, object destructuring can extract multiple properties in one statement, can access properties from nested objects, and can set a default value if the property doesn't exist.


1 Answers

You could put all of your code under one namespace like this:

var namespace = {};  namespace.someClassObj = {};  delete namespace.someClassObj; 

Using the delete keyword will delete the reference to the property, but on the low level the JavaScript garbage collector (GC) will get more information about which objects to be reclaimed.

You could also use Chrome Developer Tools to get a memory profile of your app, and which objects in your app are needing to be scaled down.

like image 142
Yochai Akoka Avatar answered Sep 27 '22 23:09

Yochai Akoka