Results 1 to 5 of 5
  1. #1
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default Q about Garbage Collector

    Just a quick one, I wrote up a practice program that creates a binary search tree. The nodes are defined like this:
    Java Code:
    private class Node {
    	int data;
    	Node parent, lchild, rchild;
    	public Node(int d, Node p, Node l, Node r) {
    		data = d;
    		parent = p;
    		lchild = l;
    		rchild = r;
    	}
    }
    Removing the nodes from the tree is handled by skipping, like this:
    Java Code:
    Node n; //the node I want to remove, assume it's initialized and has a non-null left child
    int data = n.data;
    n = n.parent;
    if(data < n.data) //since this is a binary search tree, lesser values will alway be contained in the left subtree
        n.lchild = n.lchild.lchild;
    else
        n.rchild = n.rchild.lchild;
    Now, the node I skipped is no longer referenced by any active object, but it still references it's parent and child/children. Will it be removed by the garbage collector or not?

  2. #2
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    You are right.

    When an object is no longer referenced it becomes eligable for garbage collection.
    It does not matter if the "to be garbage collected" object references another object.

    However, exactly when the garbage collection process cleans up those eligable objects is up to the JVM.

    You can suggest that garbage collection runs by System.gc(), but this is not gauranteed.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Thanks a lot!
    This program is only studying for an exam, but the question has been bugging me, it wouldn't be a very good habit to write memory leaks.

  4. #4
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    If your studying for the SCJP exam then "A programmer's guide to Java certification: a comprehensive primer By Khalid Azim Mughal, Rolf W. Rasmussen" is an excellent book. Very consise.

    Best of luck

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Nah, it's a course at my uni. And besides, reference material is quite plentiful thanks to our professor, it's just this one little tidbit that bugged me.
    Thanks again!

Similar Threads

  1. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 10:04 PM
  2. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 12:56 PM
  3. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 09:04 PM
  4. How to use the garbaje collector
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 06-29-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •