Can I trigger JavaScript's garbage collection?

trigger event javascript
javascript trigger click event
javascript trigger change event
javascript trigger click event programmatically
javascript trigger click event on element
trigger input event javascript
jquery trigger('change event)

I want to trigger JavaScript garbage collection. Is it possible? Why would I want to, or not want to, do this?

I went out on a small journey to seek an answer to one of your questions: Is it possible?

People all over town are saying that deleting the references will do the trick. Some people say that wiping the object is an extra guarantee (example). So I wrote a script that will try every trick in the book, and I was astonished to see that in Chrome (22.0.1229.79) and IE (9.0.8112.16421), garbage collection doesn't even seem to work. Firefox (15.0.1) managed without any major drawbacks apart from one (see case 4f down below).

In pseudo-code, the test goes something like this.

  1. Create a container, an array, that will hold objects of some sort. We'll call this container Bertil here on.

  2. Each and every object therein, as an element in Bertil, shall have his own array-container declared as a property. This array will hold a whole lot of bytes. We'll call any one of Bertil's elements, the object, Joshua. Each Joshua's byte array will be called Smith.

    Here's a mind map for you to lean back on:

    Bertil [Array of objects] -> Joshua [Object] -> Smith [Array of bytes] -> Unnamed [Bytes].

  3. When we've made a mess out of our available memory, hang around for a sec or two and then execute any one of the following "destruction algorithms":

    4a. Throw a delete operand on the main object container, Bertil.

    4b. Throw a delete operand on each and every object in that container, kill every Joshua alive.

    4c. Throw a delete operand on each and every array of bytes, the Smiths.

    4d. Assign NULL to every Joshua.

    4e. Assign UNDEFINED to every Joshua.

    4f. Manually delete each and every byte that any Joshua holds.

    4g. Do all of the above in a working order.

So what happened? In case 4a and 4b, no browser's garbage collector (GC) kicked in. In case 4c to 4e, Firefox did kick in and displayed some proof of concept. Memory was reclaimed shortly within the minute. With current hardcoded default values on some of the variables used as test configuration, case 4f and 4e caused Chrome to hang, so I can't draw any conclusions there. You are free to do your own testing with your own variables, links will be posted soon. IE survived case 4f and 4e but his GC was dead as usual. Unexpectedly, Firefox survived but didn't pass 4f. Firefox survived and passed 4g.

In all of the cases when a browser's GC failed to kick in, waiting around for at least 10 minutes didn't solve the problem. And reloading the entire page caused the memory footprint to double.

My conclusion is that I must have made a horrible error in the code or the answer to your question is: No we can't trigger the GC. Whenever we try to do so we will be punished severely and we should stick our heads in the sand. Please I encourage you to go ahead, try these test cases on your own. Have a look in the code were comment on the details. Also, download the page and rewrite the script and see if you can trigger the GC in a more proper way. I sure failed and I can't for the life of me believe that Chrome and IE doesn't have a working garbage collector.

JavaScript Triggers – A List Apart, is the id attribute: <div id="navigation"> <ul> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> </ul> </div> var x = document. Trigger a Button Click on Enter. Press the "Enter" key inside the input field to trigger the button: Button

You can trigger manually JavaScript garbage collector in IE and Opera, but it's not recommended, so better don't use it at all. I give commands more just for information purpose.

Internet Explorer:


Opera 7+:


Assigning an action to a button, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The trigger () method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler () method, except that triggerHandler () does not trigger the default behavior of the event.

Garbage collection runs automatically. How and when it runs and actually frees up unreferenced objects is entirely implementation specific.

If you want something to get freed, you just need to clear any references to it from your javascript. The garbage collector will then free it.

If you explain why you even think you need to do this or want to do this and show us the relevant code, we might be able to help explain what your alternatives are.

Custom Events with Vanilla JavaScript, As long as we're at it we can port the “required” trigger to a custom attribute, too. required=“true” , for instance, though any value will do because this trigger just  Fair warning: element.onclick() does not behave as expected. It only runs the code within onclick="" attribute, but does not trigger default behavior.. I had similar issue with radio button not setting to checked, even though onclick custom function was running fine.

  1. Check your code for global variables. There may be data coming through an ajax call that is stored, and then referenced somewhere and you did not take this into account.
  2. As a solution, you should wrap huge data processing into an anonymous function call and use inside this call only local variables to prevent referencing the data in a global scope.
  3. Or you can assign to null all used global variables.
  4. Also check out this question. Take a look at the third example in the answer. Your huge data object may still be referenced by async call closure.

jQuery trigger() Method, trigger() method can be used on jQuery collections that wrap plain JavaScript objects similar to a pub/sub mechanism; any event handlers bound to the object will  You can do this in the dropdownListBox by using onClientSelect. I want to trigger a javaScript function when a field in an htmlb:tableView is changed.

This answer suggests the following garbage collection request code for Gecko based browsers:


.trigger(), Such events are commonly called synthetic events, as opposed to the events fired by the browser itself. Creating custom events. Events can be  Yes, I get the click event work in the same page $("#someLink").click(function(){$("#fileInput").trigger("click");});). The file input is of hidden type. The file input is of hidden type. – Sambit Feb 4 '16 at 15:56

Creating and triggering events, I need to call a javascript function from an external js file. And I want to call it with a link. You can see a part of my code below. HTML. <a id="  2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent which take one string argument (You can assign the name to the value of this argument).: public void RaisePostBackEvent(string eventArgument) { DoSomething(); } 3- Write a script to trigger post back:

How can I call an external javascript function in html with a link?, I see people providing you with a way to SET an eventListener for 'click' in Javascript, even jQuery in some answers. If you simply want to TRIGGER a click event  Occasionally, I find need to be able to manually trigger a predefined JavaScript event from within the context of a web application. Those of you who didn’t understand that first statement may want to stop reading now or risk suffering from extreme boredom, but those who actually have encountered the same challenge in coding, please read on!

How can I trigger a click event in JavaScript?, This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript  When JavaScript is used in HTML pages, JavaScript can "react" on these events. HTML Events An HTML event can be something the browser does, or something a user does.

  • What's driving you to do this?
  • No. [comment limit]
  • Hi,actually in my application,huge data are coming through ajax call on every 20 second.Due to heave data browser get crashed after few minutes.So that i want to clear browser's memory to avoid the failure of browser.
  • The problem you should be solving is that of your browser receiving so much data in the first place.
  • This comment from BoltClock is very useless. In a simple "my company looks like this"-homepage this is in some case true, but when you develope a "software" which for example collects sensor stats live in a highstock chart, there is nothing wrong to have "huge" data.
  • I've also noticed that deleting lots of objects in V8 engine (Chrome) is a bad idea. It's simply very slow and assigning to null is much faster.
  • You said: "deleting lots of objects". Might be good to know for you that you can't delete an object in JS (nor functions or variables), only object properties and array elements. Assigning null (or undefined?) to every object reference once we're done with em should be all we need to do. IN THEORY. But as my little research demonstrated I failed utterly hard in firing up the GC in Chrome and IE. Basically, there should be hard to achieve a memory leak in managed code with its own garbage collector, for me, it was the opposite.
  • +1, possibly the BEST answer I've ever read on SO. Entertaining (but not too) way written, exactly to the spot. I'm building really dynamic data management - that possibly will use browser as a data-bridge from Google Spreadsheets and alike. So "you got too big data" will be my issue as well...
  • @MartinAndersson I went and ran your tests. Then discovered (also mentioned in the answers below), that IE honors the explicit call to "CollectGarbage()" (I added it to your case 2 pattern just by testing it out - didn't test thoroughly). While it's "not recommended", it frees the memory while waiting didn't free anything...
  • a lot of developers have problem with memory , i think google and firefox should allow manual run of their garbage collectors , i tried waiting in my script not worked , chrome GC come in action when script finish on waiting it not start
  • Hi,actually in my application,huge data are coming through ajax call on every 20 second.Due to heavy data browser get crashed after few minutes.So that i want to clear browser's memory to avoid the failure of browser
  • Code is very simple,actually we generating dynamic graph through live data that is coming on every 20 second.Problem already explained.