Can I trigger JavaScript's garbage collection?

后端 未结 9 2029
不知归路
不知归路 2021-01-31 07:17

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

相关标签:
9条回答
  • 2021-01-31 07:35

    Came across this question and decided to share with my recent findings. I've looked to see a proper handling of a WeakMap in Chrome and it's actually looks okay:

    1) var wm = new WeakMap()

    2) var d = document.createElement('div')

    3) wm.set(d, {})

    at this stage weak map holds the entry cause d is still referencing the element

    4) d = null

    at this stage nothing references the element and it's weakly referenced object, and indeed after a couple of minutes entry disappeared and garbage collected.

    when did the same but appended the element to the DOM, it was not reclaimed, which is correct, removed from the DOM and still waiting for it to be collected :)

    0 讨论(0)
  • 2021-01-31 07:38

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

        window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
          .getInterface(Components.interfaces.nsIDOMWindowUtils)
          .garbageCollect();        
    
    0 讨论(0)
  • 2021-01-31 07:44

    Yes, you can trigger garbage collection by re-loading the page.

    You might want to consider using a Factory Pattern to help re-use objects, which will greatly cut down on how many objects are created. Especially, if you are continuously creating objects that are the same.

    If you need to read up on Factory Patterns then get yourself this book, "Pro Javascript Design Patterns" by Ross Harmes and Dustin Diaz and published by APress.

    0 讨论(0)
  • 2021-01-31 07:46

    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.

    http://martinandersson.com/dev/gc_test/?case=1

    http://martinandersson.com/dev/gc_test/?case=2

    http://martinandersson.com/dev/gc_test/?case=3

    http://martinandersson.com/dev/gc_test/?case=4

    http://martinandersson.com/dev/gc_test/?case=5

    http://martinandersson.com/dev/gc_test/?case=6

    http://martinandersson.com/dev/gc_test/?case=7

    0 讨论(0)
  • 2021-01-31 07:50
    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.
    0 讨论(0)
  • 2021-01-31 07:51

    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.

    0 讨论(0)
提交回复
热议问题