What's the best way to clean up a BST for garbage collection?
I want to clean up the BST so I can take advantage of the garbage collector. So, to clean up the BST, is it enough to set the root to null so that I end up with a bunch of abandoned nodes with no pointers to them? Or is it better to set each node to null?
- Are these two equivalent methods?
- Will one of them cause a memory leak?
- Will garbage collection be faster than another?
I am also concerned about weak link and strong link etc.
+3
source to share
1 answer
Any object that cannot be reached on any thread will be eligible for garbage collection.
Based on this:
- Yes.
- Not.
- Depends on the GC implementation. In any case, as a Java programmer, you have no control over it. All you can do is trust that he will do his job just fine. Also, setting the root to zero is O (1), while zeroing all references is O (n), where n is the number of nodes.
Output:
Just set the root to null and let the GC go through it at the right time :)
+1
source to share