Can I trigger JavaScript's garbage collection?

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

    If it is true that there is no way to trigger a GC, as implied by the other answers, then the solution would be for the appropriate browser standards group to add a new JavaScript function to window or document to do this. It is useful to allow the web page to trigger a GC at a time of its own choosing, so that animations and other high-priority operations (sound output?) will not be interrupted by a GC.

    This might be another case of "we've always done it this way; don't rock the boat" syndrome.

    ADDED:

    MDN documents a function "Components.utils.schedulePreciseGC" that lets a page schedule a GC sometime in the future with a callback function that is called when the GC is complete. This may not exist in browsers other than Firefox; the documentation is unclear. This function might be usable prior to animations; it needs to be tested.

提交回复
热议问题