• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Garbage Collection

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing some exercises, I did one that I don't know the answer. ;-)

Given this exercises:

Exercise One:

Exercise Two: (Equal than before but with "hl" as static var.)

The questions are:

[1] Is in exercise one the object "t" create in line 14 eligible for GC?
[2] Is in exercise two the object "t" create in line 14 eligible for GC?

My answers:

[1] For me YES. "hl" (Highlander) is an instance of the object that GC want to "destroy". This is an "Island of isolation", so, threre are two object two "destroy".
[2] I don't know! I need some help! ;-)

Please, correct me if I'm wrong. And help for the second question!

A lot of Thanks!
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ask yourself if the static variable "hl" (Highlander) has anything to do with the concerned GC code or variable?
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the static variable "hl" (Highlander) has anything to do with the concerned GC code or variable.

So, in [1] and [2] exercises, Are both elegible for GC?
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alam Ameghino wrote:No, the static variable "hl" (Highlander) has anything to do with the concerned GC code or variable.

So, in [1] and [2] exercises, Are both elegible for GC?



I would say so.
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mmmm, I don't understand!
Please, Can you give me more details?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:

Alam Ameghino wrote:No, the static variable "hl" (Highlander) has anything to do with the concerned GC code or variable.

So, in [1] and [2] exercises, Are both elegible for GC?



I would say so.



What is this question and response for? Are you asking about the Test instances? Or the contained Highlander instances?

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, if I am not be clear.

I am asking for [1] and [2], How many object are elegible for GC alter line 16?

In [1], I supposed that 2, "t" and her "hl". Am I right?
In [2], I do not know!
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alam Ameghino wrote:Sorry, if I am not be clear.

I am asking for [1] and [2], How many object are elegible for GC alter line 16?



Simply, an object is eligible for GC, when it is no longer reachable. So...

Alam Ameghino wrote:
In [1], I supposed that 2, "t" and her "hl". Am I right?



You tell us. Are you saying that both objects are not reachable?

Alam Ameghino wrote:
In [2], I do not know!




Same question. Is either object Reachable?

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

You tell us. Are you saying that both objects are not reachable?


Yes, for me "t" and "hl" are both elegible for GC

Same question. Is either object Reachable?


The same. Yes, for me "t" and "hl" are both elegible for GC. But I have doubt because "hl" is satic var! It is important?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alam Ameghino wrote:

Same question. Is either object Reachable?


The same. Yes, for me "t" and "hl" are both elegible for GC. But I have doubt because "hl" is satic var! It is important?




Hint: Can you access a static variable of a class, without having an instance of the class?

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

Hint: Can you access a static variable of a class, without having an instance of the class?



Yes, of course. It is not private.
But, in this excercise there is not such access.
So, "hl" elegible for GC?

Henry, a lot of thanks!
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alam Ameghino wrote:
Yes, of course. It is not private.
But, in this excercise there is not such access.
So, "hl" elegible for GC?

Henry, a lot of thanks!




Sorry, I am out of hints... so here is the answer. I was actually hoping that you get it by yourself, as people tend to remember better when they do figure it out.

You can access static variables of a class without an instance. In this case, you can access the object referenced by the "hl" variable of the Test class via "Test.hl". This means that the object is still reachable, and hence, not eligible for GC.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wrong! But you help me understand! THANKS!

So,

[1] "t" and "hl" are elegible for GC.
[2] only "t" is elegible for GC.

Henrry, you are The Messi of java world! ;-)
 
Oh. Hi guys! Look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic