Results 1 to 6 of 6
  1. #1
    nolsen01 is offline Member
    Join Date
    Jul 2009
    Posts
    18
    Rep Power
    0

    Default When does the Garbage Collector pick up an object?

    I'm learning about Data Structures and Algorithms and I've started to make a doubly linked list. Here is some code that is supposed to remove a datatype from a given index:

    Java Code:
    public T removeFrom(int index) {
    		DLNode<T> indexNode = this.find(index);
    		
    		T element = indexNode.getElement();
    		
    		indexNode.getNextNode().setPreviousNode(indexNode.getPreviousNode());
    		indexNode.getPreviousNode().setNextNode(indexNode.getNextNode());
    				
    		return element;
    	}
    How do I know when "indexNode" will be picked up by the garbage collector? Will it automatically be removed from memory after this code is run?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nolsen01 View Post
    How do I know when "indexNode" will be picked up by the garbage collector? Will it automatically be removed from memory after this code is run?
    You don't know when it will be garbage collected; all you know is that it wil be collected when needed. Don't think too much about garbage collection; most attempts to 'help' it work against it in practice.

    kind regards,

    Jos

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    I had similar question the other day, which i have enquired my lecturer about. From what I have found out I'd agree with:
    most attempts to 'help' it work against it in practice
    However, it is possible to call the garbageCollector at the end of your code, I am not sure what sort of method it is tho.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FlyNn View Post
    I had similar question the other day, which i have enquired my lecturer about. From what I have found out I'd agree with:


    However, it is possible to call the garbageCollector at the end of your code, I am not sure what sort of method it is tho.
    No, all you can do is call System.gc() which is just a hint that you want the garbage collector to start running; it's up to the garbage collector to decide whether or not to actually run.

    kind regards,

    Jos

  5. #5
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    No, all you can do is call System.gc() which is just a hint that you want the garbage collector to start running; it's up to the garbage collector to decide whether or not to actually run.

    kind regards,

    Jos
    Thanks for correcting me :) I guess if you run System.gc(), you at least asking the GC to consider cleaning up the stuff. Smart thing GC isnt it. Seems like they have a mind of their own hahaha.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FlyNn View Post
    Thanks for correcting me :) I guess if you run System.gc(), you at least asking the GC to consider cleaning up the stuff. Smart thing GC isnt it. Seems like they have a mind of their own hahaha.
    Yup, as always when a computer program makes a decision we don't fully understand we assume it's either a bug or artificial intelligence. When the gc 'sees' it still has free memory enough it won't go through the trouble of collecting memory; quite clever indeed, human beings always consider trade-offs like that.

    kind regards,

    Jos

Similar Threads

  1. Q about Garbage Collector
    By m00nchile in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 05:57 AM
  2. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 09:04 PM
  3. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM
  4. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:56 AM
  5. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:04 PM

Posting Permissions

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