This week's book giveaway is in the Cloud forum.
We're giving away four copies of The Business Blockchain and have William Mougayar on-line!
See this thread for details.
Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on question #6 self test chapter 5 book and cd [K&B7]

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will immediately start by saying that I've read this thread ( GC ) and I thought I understood the operation of the GC but maybe not.
Given this code:


the answers are two and must indicate between:



My choices were A and E, but the correct answer is C and F.
I said A because I think that the objects are three (array associated to the reference "da", the object Dozens added to da[0] and other object associated to the reference Dozens "d").
And I said E because it seems that the only object that is no longer accessible is that referenced by "d".

Do you help me to understand?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:My choices were A and E, but the correct answer is C and F.
I said A because I think that the objects are three (array associated to the reference "da", the object Dozens added to da[0] and other object associated to the reference Dozens "d").
And I said E because it seems that the only object that is no longer accessible is that referenced by "d".

Do you help me to understand?

With this kind of questions it's of course quite normal if you have the number of created objects wrong, you'll probably be wrong on the objects eligible for GC as well.

I'll give you a hint and then you can try this question again. You are indeed correct that the array referred by reference variable da is an object. Because as you (should) know: every array is an object! Repeat after me: every array is an object, every array is an object, every array is an object. And now have another look at the Dozens class.
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,

I too got this question wrong on my first attempt. It wasn't until I diagramed it out, as Roel has suggested in previous posts, that it made sense to me. FYI, I didn't use the whiteboard and marker they provided during the OCA exam in either of my two previous attempts (a huge mistake I won't be making again) and my scores reflected just how well I can keep count of objects and keep track loop variable values in my head.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
John Lerry wrote:My choices were A and E, but the correct answer is C and F.
I said A because I think that the objects are three (array associated to the reference "da", the object Dozens added to da[0] and other object associated to the reference Dozens "d").
And I said E because it seems that the only object that is no longer accessible is that referenced by "d".

Do you help me to understand?

With this kind of questions it's of course quite normal if you have the number of created objects wrong, you'll probably be wrong on the objects eligible for GC as well.

I'll give you a hint and then you can try this question again. You are indeed correct that the array referred by reference variable da is an object. Because as you (should) know: every array is an object! Repeat after me: every array is an object, every array is an object, every array is an object. And now have another look at the Dozens class.


As always your suggestions are useful.
I try again.

The objects are actually 5 and that is:
- array associated to the reference "da".
- object placed in position 0 in the "da".
- array defined as an instance variable to the previous object.
- object associated to the reference "d".
- array defined as an instance variable to the previous object.

The objects eligible for GC are 2 because, in addition to the object "d" no longer accessible, there is also its instance variable that is an array.

Is it correct?
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Brumpton wrote:John,

I too got this question wrong on my first attempt. It wasn't until I diagramed it out, as Roel has suggested in previous posts, that it made sense to me. FYI, I didn't use the whiteboard and marker they provided during the OCA exam in either of my two previous attempts (a huge mistake I won't be making again) and my scores reflected just how well I can keep count of objects and keep track loop variable values in my head.


Can I use a sheet and a pen at the exam?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Is it correct?

You are spot-on!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Can I use a sheet and a pen at the exam?

You can not bring anything of your own during the exam. So you must leave everything outside the room you are taking the exam (not even a bottle of water). But the testing center is required to provide you with some writing materials. Back in the days, a few sheets of paper and a pen. Currently it's mostly a whiteboard and an erasable marker. When I took the OCA exam in January 2015 I got a few laminated (plastified) papers and an erasable marker, which was fine for me (I don't write much).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Brumpton wrote:FYI, I didn't use the whiteboard and marker they provided during the OCA exam in either of my two previous attempts (a huge mistake I won't be making again) and my scores reflected just how well I can keep count of objects and keep track loop variable values in my head.

How can you be that cocky after failing the 1st time? Most loops have a loop counter and different variables you have to monitor, mixed with a few arithmetic operations and if conditions. That's just crazy trying to do without making a few notes. Although I don't write much during the exam, I always keep track of loop variables using the provided writing materials.
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you be that cocky after failing the 1st time?


Certainly not cocky... I was rushed to attempt the exam a second time because I didn't want to have to take another school term. I knew very quickly on my second attempt that I was not prepared to take the exam. You can't write down what you don't know.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have another question on GC.

Given this code:



The objects eligible for GC are 3 because as once set the last reference to null (e1=null) automatically are set to null also reference "internal" to the objects that link to other objects (eg. e1.e=e3 )?
 
Henry Wong
author
Marshal
Pie
Posts: 22085
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
The objects eligible for GC are 3 because as once set the last reference to null (e1=null) automatically are set to null also reference "internal" to the objects that link to other objects (eg. e1.e=e3 )?


The JVM doesn't use reference counting. Eligibility for GC is determined by reachability. When an object is no longer reachable, it will be eligible to be GC'ed. It doesn't matter if those three, now unreachable objects, reference each other.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:The objects eligible for GC are 3 because as once set the last reference to null (e1=null) automatically are set to null also reference "internal" to the objects that link to other objects (eg. e1.e=e3 )?

Consider this code snippet:When line1 is fnished, both object1 and object2 are eligible for GC, because both objects aren't reachable anymore.

Now have a look at this code:When line1 is fnished, only object1 is eligible for GC, because object2 is still reachable using reference variable e2. When line2 is fnished, object2 is also eligible for GC, because it isn't reachable anymore.

Hope it helps!
Kind regards,
Roel

(PS. This practice question is a really good example to test your drawing skills to reveal the correct answer )

[edit] fixed typo (Thanks Mushfiq)
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Consider this code snippet:When line1 is fnished, both object1 and object2 are eligible for GC, because both objects aren't reachable anymore.


Then when you reach the line 1 is as if it were set to null also the reference e1.e fledged object2 also no longer reachable. Is it correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Then when you reach the line 1 is as if it were set to null also the reference e1.e fledged object2 also no longer reachable. Is it correct?

Because object2 (which was referenced by object1.e) isn't reachable anymore (after line1 is finished), it's also eligible for GC although object1.e is still referring to object2. This last statement can easily be verified by using a temporary reference variableOutput:
(Lost@70c787d7 Lost@3ba76e19) (Lost@70c787d7 Lost@3ba76e19)
null (Lost@70c787d7 Lost@3ba76e19)


Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
John Lerry wrote:Then when you reach the line 1 is as if it were set to null also the reference e1.e fledged object2 also no longer reachable. Is it correct?

Because object2 (which was referenced by object1.e) isn't reachable anymore (after line1 is finished), it's also eligible for GC although object1.e is still referring to object2. This last statement can easily be verified by using a temporary reference variableOutput:
(Lost@70c787d7 Lost@3ba76e19) (Lost@70c787d7 Lost@3ba76e19)
null (Lost@70c787d7 Lost@3ba76e19)


Hope it helps!
Kind regards,
Roel


so in the current situation no object will be eligible for GC but if we set the reference temp again to null in that case there will be two objects eligible. Is it correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:so in the current situation no object will be eligible for GC but if we set the reference temp again to null in that case there will be two objects eligible. Is it correct?

Yes, that's correct!
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:It doesn't matter if those three, now unreachable objects, reference each other.

Henry


if it does not matter if the objects referencing each other why the objects pointed to by e3 and e2 are not immediately eligible for GC? are no longer reachable because the reference e3 and e2 are set to null.



 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:if it does not matter if the objects referencing each other why the objects pointed to by e3 and e2 are not immediately eligible for GC? are no longer reachable because the reference e3 and e2 are set to null.


Because they are still referenced through reference variable e1:
  • e1 refers to object1 (obviously)
  • object3 is referenced via reference variable e of object1 (see line2)
  • object2 is referenced via reference variable e of object3 (see line1)


  • Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But also by setting the reference e1 to null the situation does not change because those references will always be present. I can not understand this concept.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:But also by setting the reference e1 to null the situation does not change because those references will always be present. I can not understand this concept.

    Before e1 is null, you can write this code to print object1 and object3:Now it's up to you to write similar code to print those objects after e1 is set to null. Ready, set, go!
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's proceed step by step.
    In this case:


    the output is:

    Lost@232204a1 Lost@4aa298b7 Lost@7d4991ad Lost@232204a1



    the only object whose reference is set to null (e3) is not eligible for GC because it is still pointed to by reference e1.e.
    if I tried this statement:


    I would get a NullPointerException because it is as if it had been set to null also the reference e3.e.
    That said, for the same reason, if I do not comment line 1 by setting the reference to null e2 is as if it were set to null also the reference e2.e and then the first object eligible would be referenced by e2 and this thing, if I'm not mistaken, not is true.
    This is now my doubt.



     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:if I tried this statement:


    I would get a NullPointerException because it is as if it had been set to null also the reference e3.e.

    You'll get a NullPointerException, because e3 is null NOT because the reference variable e of object3 is null. And you can easily confirm this statementOutput:
    e3 = Lost@a52f9b2 | e3.e = Lost@4d871a69
    e1.e = Lost@a52f9b2 | e1.e.e = Lost@4d871a69


    Before we proceed, let's see if you understand why both lines print exactly the same. This code proves that although the reference variable e3 was set to null, the reference variable e of object3 (the actual object reference variable e3 initially - before set to null - referred to) is still referring to another object (which is the same object reference variable e2 is referring to) and is not set to null as well.
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, now I understand that if e3 is set to null is NOT set to null also e3.e.
    I tried to make a drawing to outline the situation.
    I agree on the fact that now there is no object eligible for GC because the object pointed to by e3 is reached by e1.e.
    The situation does not change when it is set to null also e2 because the object pointed to by e2 is still reached by e3.e.
    But what I do not understand is why setting to null also e1 automatically all 3 objects become eligible. In reality the situation is not expected to change as object1 would still be reached by e2.e, object2 would still be reached by e3.e and object3 would still be reached by e1.e.
    scansione0001.jpg
    [Thumbnail for scansione0001.jpg]
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:Ok, now I understand that if e3 is set to null is NOT set to null also e3.e.
    I'm glad to hear you understand now.

    John Lerry wrote:I tried to make a drawing to outline the situation.


    John Lerry wrote:I agree on the fact that now there is no object eligible for GC because the object pointed to by e3 is reached by e1.e.
    That's half of the reason why e3 is not eligible for GC. The other part is that reference variable e1 is still reachable!

    John Lerry wrote:The situation does not change when it is set to null also e2 because the object pointed to by e2 is still reached by e3.e.
    You are correct! When you set e2 to null, there is still no object eligible for GC. Because object2 is still reachable through the reference variable e of object3 AND (this part is very crucial) object3 is still reachable through the reference variable e of object1 AND (again very crucial) object1 is still reachable through the reference variable e1. So all objects (object1, object2 and object3) can be reached using reference variable e1.

    John Lerry wrote:But what I do not understand is why setting to null also e1 automatically all 3 objects become eligible. In reality the situation is not expected to change as object1 would still be reached by e2.e, object2 would still be reached by e3.e and object3 would still be reached by e1.e.
    If e1 is also set to null, none of the objects are reachable anymore because reference variable e1 was the one and only reference still available to reach all objects. So e1 was your starting point to reach all 3 actual objects, but because e1 is set to null your starting point is gone and thus you can't reach any of the actual objects making them all eligible for GC.

    Let's compare this situation with a non-programming example. Assume you live in Italy and a 3 of your relatives live on the island Sicily. These are represented using the actual objects object1, object2 and object3. At each of your relatives' houses you have another means of transport: e.g. a bike, a car and a horse. Using these means of transport you can visit all your relatives once you get to one of your relatives' houses. These means of transport are represented using the reference variable e in every actual object (e.g. [e1] of object1 is the bike). So once you'll get to the house of one of the relatives, you always can visit all of them. But you are still in Italy and thus have to cross the Mediterranean Sea. Again you'll have 3 possibilities: a ferry, a plane and a helicopter. These possibilities are represented using the reference variables e1 (ferry), e2 (plane) and e3 (helicopter).
    So at the beginning you can take any of these 3 possibilities to cross the sea and visit all your relatives. Keep in mind, that when you can't visit a relative anymore, the relative (actual object) is eligible for GC.
    Then the helicopter got completely destroyed by a very, very angry employee (e3 is set to null). Luckily you still can use the other 2 possibilities to cross the sea and visit all your relatives; so no relatives (actual objects) are eligible for GC. A few days later there's a very big volcanic eruption, which makes all air traffic in Europe impossible! So you can't use the plane anymore to cross the sea (e2 is set to null). But no problem, with the boat you can still cross the sea and visit all your relatives; so no relatives (actual objects) are eligible for GC. But the next day all ferry workers go on strike, because of the poor working conditions (e1 is set to null). So now you can't cross the Mediterranean Sea anymore (swimming is not an option) and thus you can't reach/visit any of your relatives (actual objects). And because you can't visit one relative (actual object), you can't visit any of your relatives (actual objects) => all relatives (actual objects) are eligible for GC.

    Hope it helps!
    Kind regards,
    Roel
     
    John Lerry
    Ranch Hand
    Posts: 145
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Let's compare this situation with a non-programming example. Assume you live in Italy and a 3 of your relatives live on the island Sicily. These are represented using the actual objects object1, object2 and object3. At each of your relatives' houses you have another means of transport: e.g. a bike, a car and a horse. Using these means of transport you can visit all your relatives once you get to one of your relatives' houses. These means of transport are represented using the reference variable e in every actual object (e.g. [e1] of object1 is the bike). So once you'll get to the house of one of the relatives, you always can visit all of them. But you are still in Italy and thus have to cross the Mediterranean Sea. Again you'll have 3 possibilities: a ferry, a plane and a helicopter. These possibilities are represented using the reference variables e1 (ferry), e2 (plane) and e3 (helicopter).
    So at the beginning you can take any of these 3 possibilities to cross the sea and visit all your relatives. Keep in mind, that when you can't visit a relative anymore, the relative (actual object) is eligible for GC.
    Then the helicopter got completely destroyed by a very, very angry employee (e3 is set to null). Luckily you still can use the other 2 possibilities to cross the sea and visit all your relatives; so no relatives (actual objects) are eligible for GC. A few days later there's a very big volcanic eruption, which makes all air traffic in Europe impossible! So you can't use the plane anymore to cross the sea (e2 is set to null). But no problem, with the boat you can still cross the sea and visit all your relatives; so no relatives (actual objects) are eligible for GC. But the next day all ferry workers go on strike, because of the poor working conditions (e1 is set to null). So now you can't cross the Mediterranean Sea anymore (swimming is not an option) and thus you can't reach/visit any of your relatives (actual objects). And because you can't visit one relative (actual object), you can't visit any of your relatives (actual objects) => all relatives (actual objects) are eligible for GC.

    Hope it helps!
    Kind regards,
    Roel


    I make you the compliments, with your example you made me understand the concept. You could not choose a more appropriate example.
    Thank You.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    John Lerry wrote:I make you the compliments, with your example you made me understand the concept. You could not choose a more appropriate example.

    Very glad to hear you understand the concept now. And very happy you liked the real world example as well
     
    nemo zou
    Ranch Hand
    Posts: 35
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    object2 is referenced via reference variable e of object3 (see line1)


    Does this mean that object2 is referenced via e1.e.e?
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    nemo zou wrote:
    object2 is referenced via reference variable e of object3 (see line1)


    Does this mean that object2 is referenced via e1.e.e?

    Yes! That can be easily verified by adding a simple print-statement likeOutput: Lost@2733219b Lost@2733219b true -> clearly the same objects!
     
    nemo zou
    Ranch Hand
    Posts: 35
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    nemo zou wrote:
    object2 is referenced via reference variable e of object3 (see line1)


    Does this mean that object2 is referenced via e1.e.e?

    Yes! That can be easily verified by adding a simple print-statement likeOutput: Lost@2733219b Lost@2733219b true -> clearly the same objects!


    Thanks! This makes me clear~
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic