简体   繁体   中英

Do I have to delete the node or the garbage collector does the work in JS?

To delete the last node of the circular link list in JS, I will traverse to the second last node and I will link the second last node with the first node, the last node is now disconnected from the chain. Now is it necessary to delete the last node like in c++, doesn't garbage collector of JS does the work?

JavaScript handles freeing memory through the use of the garbage collector. As long as there are no longer any references pointing to an object, where all references have moved out of scope, it will be garbage collected. You don't need to worry about freeing memory yourself.

One thing to note in your use case is the limitation of circular references . Specifically when you have a situation where object A references object B , A->B ; and object B references object A , B->A . Even if both A and B move out of scope and nothing else references them, they will not be garbage collected as both object still have "1 reference" from eachother.

I believe "most" modern browsers and runtime environments now support Mark & Sweep , and if they're not accessible should also be garbage collected, solving this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM