memory - Do I need to manually dispose of a java tree structure when it is no longer needed? -


i construct lots of object trees in applications, each node typical tree node (reference parent , list of references children). these trees temporary, meaning might dispose of them before application terminates.

so far i've added method tree node class able recursively traverse tree branch , "destroy it" (set parent reference null , clear children list, etc.).

public void destroy() {     (node node : children) {         node.destroy();     }     parent = null;     children.clear(); } 

this made sense me, since null-ing reference root of tree have stored somewhere not enough - children might still have reference it, meaning stay in memory , cause memory leak. correct in assuming , providing such method?

the reason why i'm doubting myself see such methods in apis provide tree structure support (at least not directly in tree node interfaces). proper pattern dealing such cases?

you not need destroy or clean objects yourself.

you need make sure there no references them live objects (live sounds clear rather complex definition).

note if there curricular references in unneeded objects not need care them, gc handle this.

related:

does assigning objects null in java impact garbage collection?

can class nullified within class itself?

is necessary nullify objects in junit teardown methods?


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -