This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Garbage Collection Confusion

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the following sample code:



Which statement is true about the objects referenced by a, b and amain, after line 16?

A. None of these objects are eligible for garbage collection.
B. Only the object referenced by b is eligible for garbage collection.
C. Only the object referenced by amain is eligible for garbage collection.
D. Only the object referenced by a is eligible for garbage collection.
E. The objects referenced by a and b are eligible for garbage collection.
F. The objects referenced by a,b and amain are eligible for garbage collection.

Doubt: i think the answer should be F, as a and b form Island of Isolation and at line 16, we are nulling the reference for amain reference. Please clarify the answer.

Source: self-doubt

thanks,
Gitesh
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i think on the heap we have two Object (OA and OB )and two instance variable a and b.
Both a and b are referring to one Object each and also amain is also referring to the one Object

So a and amain --- > OA
b ---> OB

we have only one way to get something from heap, way goes via amain (on stack), making this null leaving nothing for us..so let the garbage collector swipe off Island of Isolation..they deserve to be destroyed..!!

Jokes apart ..I think you are correct...
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunny,

On my second thought while going through the question I found that at line 12,we have a=aa,

so at that time, do we have a picture like this?



and after line 16, we have:



From above we still have a reachable reference to Object OA, leaving only the Island of Isolation with a and b, making Option E as the answer..

Well can we have a discussion on this type of question to help out with Much of confusion

Thanks,
Gitesh
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Gitesh,

You were initially correct!


Originally posted by Gitesh Ramchandani:
Hi Sunny,

On my second thought while going through the question I found that at line 12,we have a=aa,

so at that time, do we have a picture like this?



Here, "aa" is a local variable to the constructor. Why do you bother about it? what you need to worry about this is , the value of 'aa' being assigned to 'a'.

So 'aa' does NOT come into picture at all.

As you have told, both the references to a and b form an island of isolation and the explicit reference to reach the object of a (through 'amain') is NULLified, you leave both the objects being eligible for garbage collection.

Here there are only two objects created. Please keep that in mind. There are two new one in the main() method for amain reference variable and another one in the Constructor of class A for b reference variable.

Does that help?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan,

So basically a and amain refer to the same object? Am I write in that? While b is a different object, is that right? So eventualy how many objects are available for grabage collection, I'm sure amain is one of them and b is one of them, but the option for b and amain is absent.

Thanks,

Akshay
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghavan,

Thanks. So is the option F is correct finally? :roll:

Gitesh
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Akshay Dashrath:
Raghavan,

So basically a and amain refer to the same object? Am I write in that?



Yes.Obviously.

Because, inside the class A's constructor you invoke new for instantiating a B object by passing this. That 'this' is nothing but the current object in execution.

Here it is getting passed as 'aa' in B() constructor and the same is assigned to the 'a' instance variable of B.



Hope it will be clear now. Am I?


While b is a different object, is that right?



Yes, very true! The reference variable "b" points to a different object of type B.


So eventualy how many objects are available for grabage collection, I'm sure amain is one of them and b is one of them,



Perfect!


but the option for b and amain is absent.



Where? they are present in the option right?

As such, there are only two objects.

  • An object of type A
  • An object of type B


  • The object of type A is being referred by two reference variables. One is an explicit reference variable "amain" from the main() method. The other one is the implicit reference variable "a" from B's instance.

    The object of type B is being referred by the instance reference variable (of course implicit) "b" of class A.

    You set the external (explicit) reference "amain" to the object of type A to NULL. Thereby you do NOT get an access to the object of type A and it does NOT have any active, external references as such.

    Whereas the internal references to the objects of type A and B form an isolation of island and they are eligible for GC.

    By looking at the answer, the objects are being referred by a,b and amain are eligible for GC

    I hope it is clear now!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Gitesh Ramchandani:
    Hi Raghavan,

    Thanks. So is the option F is correct finally? :roll:

    Gitesh



    To the best of my knowledge, it IS
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please have a look at this picture for a better understanding!



    Hope this helps!
     
    Akshay Dashrath
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Raghavan

    Wonderful explanation. Its all clear.

    Thanks,

    Akshay
     
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi All this is typically a case of circular referencing. So neither of the
    objects will be garbage collected, so we can consider it a memory leak.
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [sandipan chakrabarti]: Hi All this is typically a case of circular referencing. So neither of the objects will be garbage collected, so we can consider it a memory leak.

    No, both objects are eligible for GC. That was the point of all the discussion above. Circular references do not prevent garbage collection in Java. There is no memory leak here.
    [ January 17, 2008: Message edited by: Jim Yingst ]
     
    Gitesh Ramchandani
    Ranch Hand
    Posts: 274
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Raghavan...u Rock !!!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Gitesh Ramchandani:
    .. 3 Objects, a, b and amain are garbage Collected.



    If you have been carefully looking at the explanations and the pictorial representation, there are only ONLY two objects and NOT 3.


    Out of the 3, two of them (a and b) form Island of Isolation.



    a, b are not objects and they are the reference variables to the objects of type A and B.
     
    Gitesh Ramchandani
    Ranch Hand
    Posts: 274
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Raghavan,

    I made a mistake, even after such a Greta explanation by you...

    Gitesh
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Gitesh Ramchandani:
    ..I made a mistake, even after such a Greta explanation by you...



    No issues Gitesh. We really learn through mistakes

    Lets keep learning
     
    Deepak Chopra
    Ranch Hand
    Posts: 433
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Guys,

    Can someone tell me then what is the answer E or F?

    As only two objects are eligible for garbage collection one is being referred by a and one by b..question stated that how many objects after line 16? since after line 16 amain is not referring to any Object so we can not consider it..so i think answer is

    Object referred by a and b are eligible for garbage collection..(E)..!!


    Please reply if I am wrong?
     
    Jim Yingst
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Option F is nonsense because after line 16, variable "amain" does not refer to anything - it's null. E is correct.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Jim Yingst:
    Option F is nonsense because after line 16, variable "amain" does not refer to anything - it's null. E is correct.



    True. But still, "amain" was definitely referring to the object of type A sometimes back. IMHO, the option F seems to be an eligible candidate in that case.

    Just because a reference variable is set to null, does that mean that the object being pointed by this reference variable is NOT counted or what?

    That's what the question is basically asking about.

    Correct me if i am wrong.
     
    Ranch Hand
    Posts: 129
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If we are asked to pick, "all that applies" option E & F are correct.
    But what is the correct answer if we are allowed to pick only 1 option. Somebody please explain.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Dean Jones:
    If we are asked to pick, "all that applies" option E & F are correct.
    But what is the correct answer if we are allowed to pick only 1 option. Somebody please explain.



    That is very true.

    As per my knowledge and opinion, the option F seems to be correct as "amain" reference variable was a perfect candidate to hold an object which is now eligible for GC.

    I am not very sure though. It would be great if some experts clarify here!
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    I don't know whether i am correct... But please do look into it.

    According to me, its correct that only two objects are created ie amain and b.

    But coming to the Garbage collected objects. An object becomes eligible for garbage collection only when there are no more reachable references to it. So as the amain is made null at line 16, amain can only be capable of garbage collection..

    So i think Option C is correct.

    Please do reply, even if this is wrong....
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy Chintamani Suresh,

    Welcome to Javaranch

    Actually amain, a and b are all references and NOT objects. If you have read my previous reply, the same object being referenced by "amain" is assigned to "a" also.

    In that case, option C is definitely wrong!
     
    Chintamani Suresh
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Ya, now i am a bit clear....

    but again i think last option is correct.

    Because in this case as per the islands of isolation 'a' & 'b' can be garbage collected. As 'amain' is explicitly made null, it can also be garbage collected...

    Am i right...this time...

    Anyway thank you all for such a wonderful explanation
     
    Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
    ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
    https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
      Bookmark Topic Watch Topic
    • New Topic