• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage Collector

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming

At what point the object created in line 1 eligible for GC?
1. After line 1
2. After line 2
3. After line 3
4. After line 4
5. Object not eligible
6. It's not possible to know for sure.
The answer says 1. (after line 1), but I think it is option 2 (after line 2). Any comments?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two objects created when you run line 1. The first is created in line 1 and is immediately eligible for garbage collection (the, "new X()" inside the parenthesis). The other one is created in the doStuff2 method and is what is stored in the variable "x" on line 1. So "1" is the correct answer.
Why do you think the answer is "2"?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is your post 10000!!! congratulations!!!
now, back to work, I'll analize your answer.
chao
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Thomas]
Why do you think the answer is "2"?

Because you're actually passing x as a parameter to doStuff() method. This method invokes doStuff2()but it creates another object.

in this method, m2 has been discarded, that's why I thought option 2 was valid.
But I do get your point. the new X() in the parentheses is eligible immediately. But I thought the question was referring to this "x":
X x = doStuff(new X() );
Their answer is the following (I didn't want to post it from the beginning because I wanted to read opinions)


1 is correct. A copy of a reference to the line 1 object is passed to the doStuff()
method. the doStuff2() method abandonds the line 1 object and creates a new object,
returning a reference to the new object, which ultimately gets passed back to x on
line 1


I found this answer even more confusing. They're answering what I thought from the beginning but selecting a different option.
what do you reckon?
[ July 26, 2003: Message edited by: Andres Gonzalez ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind... I just noticed that line 1 is invoking doStuff() method as well. I got really confused with this question.
Yes, option 1 is the correct one.
apologies
[ July 26, 2003: Message edited by: Andres Gonzalez ]
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andres -
You're gobbling up my GC questions!
The other recent GC post by Majohnad outlines a 'sure-fire' technique for solving this type of problem. Draw the ref. vars, draw the objects, connect them, and disconnect them as appropriate! This problem can be solved exactly the same way as the problem in the other thread. Try that and see if the problem still confuses you.
-Bert
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert. I was also not clear before, but now I am. The Object created insided the parenthis in line 1 does not have any more references as dostuff2(..) is not using the passed copy of the reference.
Prasad
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert. I read that post, it's pretty good. I normally do that, but I don't know why I skipped this line
X x = doStuff(new X() ); // line 1
as I said before this line is actually invoking doStuff() method, but I thought everything started from line 2.
thx again
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic