Bilal and Olga
Bilal and Olga

Reputation: 3251

Do I need to explicitly destroy JavaScript objects on window unload?

I have a JavaScript widget that is hosted on websites. This widget tracks state in a number of variables in its local namespace. Moreover, it attaches listeners for several events, such as mouse movement.

Should I explicitly destroy both state-tracking variables and detach event listeners on window unload? Or is it ok to rely on the browser to do a good job of cleaning up after the user leaves the page that hosts my widget?

Upvotes: 1

Views: 3079

Answers (1)

Robusto
Robusto

Reputation: 31913

Once you get rid of the window you get rid of everything in it. Hence, no need to do your own garbage collection. If a certain browser has a memory leak, that is not your fault and your de facto garbage collection probably wouldn't help anyway.

Upvotes: 4

Related Questions