Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Question about Garbage Collector

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Question about Garbage Collector

    Hi guys, first of all I want to apologize for my bad english and I'm not even sure if this is the proprely thread to submit my question.
    That's my question:
    In this code, which one of the 3 objects i1,i2,i3 IS NOT elegible for the Garbage Collection(And why) when the method g() starts:
    class I {
    private I ref;
    private String name;
    public I(String s) {
     name = s;}
    public void referTo ( I i) {
    ref = i;
    }
     
    Class J {
    private I i1 = new I("o1");
    private I i2 = new I ("o2");
    private I i3 = new I ("o3");
    private void f() {
    i1.referTo(i2);
    i2.referTo(i1);
    i3.referTo(i3);
    i3=i1;
    i2=i1;
    g();
    }
    private void g() { }
    public static void main (String[] args) {
    new J().f();
    }
    }

    Options are
    A= o1
    B= o2
    C= o1 and o2
    D= o3
    E= None of the above
    I thank you in advance,
    Greats, Yuri.
    Last edited by Yuri0202; September 12th, 2014 at 01:50 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Question about Garbage Collector

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    We won't do your homework for you. If you'd like to give us your answer and ask us what we think about it or if you're right, we might give our opinions, but I'm not guaranteeing that either.

    Also posted here.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Question about Garbage Collector

    You are right.
    This question was on my Java exams, i took A, o1 but I think it may be wrong. I just focussed my attention to this code lines: "i3=i1, i2=1", I haven't studied GC very well, but I seem to remember that if we got two objects , x and y, and we do : x =y, than x loses his referments and he's elegible for the Garbage Collection. So in this case i3=1, i2=1, it seems like i1 is the only object not elegible for GC, and that's why I answered o1. But then I realized, it should have been i1 in the answer(instead of o1?) and still, does the method referTo influences my procediment? Well i guess they do, i don't know why but i think my answer is more likely wrong.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question about Garbage Collector

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/92651-question-about-garbage-collector.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. The Following User Says Thank You to copeg For This Useful Post:

    Yuri0202 (September 12th, 2014)

Similar Threads

  1. Question about garbage collection
    By dudushr in forum Java Theory & Questions
    Replies: 6
    Last Post: October 31st, 2013, 02:22 AM
  2. Replies: 1
    Last Post: June 12th, 2013, 12:58 PM
  3. [SOLVED] FileReader.read() reads garbage
    By Hackworth in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 27th, 2010, 01:06 PM
  4. Java Garbage Collection and destructors
    By riddhik84 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 1st, 2009, 09:06 AM
  5. Garbage Collection?
    By kalees in forum Collections and Generics
    Replies: 6
    Last Post: September 23rd, 2009, 03:07 AM