Can I trigger JavaScript's garbage collection?

后端 未结 9 2036
不知归路
不知归路 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: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.

提交回复
热议问题