0

considering the following class

public class Holder{
 Holder child;

 /* some other variables */

 Holder(boolean hasChild){
    child = ( hasChild ) ? new Holder(false) : null;
}
/*some other functions 
.
.
*/

and in the Java program i make a new object that has a child like this:

Holder parent = new Holder(true) // making it having a child Holder

The Question : Now how i can delete that holder parent and all it's children from the memory is it enough to just do

parent = null

i'm not sure if that also makes all children null too !

Why i need this ? :

i really need this answer i'm new to programming and i'm trying to make a binary tree but when i delete a node in the tree i want to make sure that all it's child nodes are also deleted from the memory ... i hop that helps in giving me a c=good answer ...

Eboubaker
  • 618
  • 7
  • 15
  • 1
    You probably shouldn't even have `foo = null;` – DontKnowMuchBut Getting Better Dec 22 '18 at 15:04
  • 2
    It depends on what you mean by delete from memory. What you can do in Java is delete all references to an object — which is what you do in your code — and then let the garbage collector decide if and when it sees fit to claim and recycle the space that your object/s occupied. You cannot and should not do more than what you are doing in your code. You can read about garbage collection in Java in many places, just search. – Ole V.V. Dec 22 '18 at 15:06
  • Agreed on all the technical comments, including the part about not needing to explicitly null. Basically once the tree goes out of scope, nothing in the main thread of the program is referring to it and it will be gc'd (edit: if necessary, as per Ole). It's not like C/C++ where it will hang around like a memory leak. – sparc_spread Dec 22 '18 at 15:08
  • @DontKnowMuchButGettingBetter Thank you for referencing that i tried to look for it but i couldn't .. i knew that there was a one somewhere here... – Eboubaker Dec 22 '18 at 15:10
  • 1
    You shouldn’t care. When your nodes are deleted from your tree as they should, be happy and trust that the garbage collector will take care of everything else. – Ole V.V. Dec 22 '18 at 15:11
  • @OleV.V. but does it takes a lot of time to clear that garbage ... like if my tree has gotten bigger and i made it automaticly starts deleting unused nodes from the tree to free some space for others ... will that take a lot of time ? like > 5 sec or it is less ? – Eboubaker Dec 22 '18 at 15:14
  • 1
    There is no such thing as a node (or any other object) being `null`. A *reference* to an object can be `null`. If that reference was the only or the last reference to the object, the object becomes *unreachable* and thereby *eligible* for garbage collection. – Ole V.V. Dec 22 '18 at 15:14
  • I repeat, don’t worry until you observe a problem. For programs we write for school or the like, even with a tree of 10 000 nodes, the garbage collector probably will not have to do any work, so it literally won’t take time. And even if it has to, it can work in parallel with your program, so you probably won’t notice. In very big servers in the industry, garbage collection time may be noticed. – Ole V.V. Dec 22 '18 at 15:20

0 Answers0