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

Objects eligible for garbage collection

 
Tomasz Rycerz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is my first post here. I'm preparing to 1Z0-803 exam. I have one question to the mock exam from Mala Gupta's OCA Java SE7. The question 45 says:

Examine the following code and select the correct statements (choose 2 options):



a At least two objects are garbage collected on line 1.
b At least one object is garbage collected on line 1.
c No objects are garbage collected on line 1
d The number of objects that are garbage collected on line 1 is unknown.
e At least two objects are eligible for garbage collection on line 2.

The correct answers are d) and e). My problem is with the explanation to option e):

"Option (e) is correct. If you marked this option incorrect, think again. The question wants you to select the correct statements, and this is a correct statement. You may argue that at least two objects were already made eligible for garbage collection at line 1, and you are correct. But because nothing changes on line 2, at least two objects are still eligible for garbage collection"

Why it says, that two objets were already made eligible for garbage collection at line 1? I think it is only one (Artist() reffered by variable a2). Could anybody correct me and tell me which additional object is eligible for garbage collection?

Thanks,
Tomasz
 
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
Hi Tomasz Rycerz,

First of all, a warm welcome to CodeRanch!

Tomasz Rycerz wrote:This is my first post here.

Hopefully still many to come

Tomasz Rycerz wrote:The correct answers are d) and e). My problem is with the explanation to option e):

"Option (e) is correct. If you marked this option incorrect, think again. The question wants you to select the correct statements, and this is a correct statement. You may argue that at least two objects were already made eligible for garbage collection at line 1, and you are correct. But because nothing changes on line 2, at least two objects are still eligible for garbage collection"

You can only determine which objects are eligible for GC. There is no guarantee at all, that an object (which is eligible for GC) will be GCed at any given line, so you NEVER know how many objects will be garbage collected. Why? Because you don't know when the garbage collector runs. That's important to know for the exam and that's what this question is about.

That's why answer (d) is correct. At line 1 there is 1 object eligible for GC (namely the object created on the 2nd statement of the main-method), but is this object really garbage collected? You don't know! You simply can't know, because you don't know when garabage collector will run and fulfill its duty. When the main method finishes (at line 2) 1 other object will be eligible for GC (namely the object created on the 1st statement of the main-method). So in total 2 objects will be eligible for GC and that's why answer (e) is correct as well.

Note: answers (a) until (d) are all about objects which are garbage collected (so actually removed from memory), answer (e) is the only one about objects being eligible for GC (so will be removed from memory if garbage collector runs).

Hope it helps!
Kind regards,
Roel
 
Paul Clapham
Sheriff
Posts: 21546
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, Roel,

You may argue that at least two objects were already made eligible for garbage collection at line 1, and you are correct.


I'm with Tomasz in saying this statement is wrong. Only one object was made eligible for GC there, no?
 
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
Paul Clapham wrote:
You may argue that at least two objects were already made eligible for garbage collection at line 1, and you are correct.


I'm with Tomasz in saying this statement is wrong. Only one object was made eligible for GC there, no?

That's of course true! My mistake! I was focusing too much on the difference between objects being eligible for GC and objects which are GCed and this slipped through the cracks. There's no doubt at all: at line 1 just 1 object is eligible for GC. I mentioned it in my previous post as well, but didn't think anymore about the wrong sentence in the explanation:
Roel De Nijs wrote:At line 1 there is 1 object eligible for GC (namely the object created on the 2nd statement of the main-method)


I will notify the author about this thread, so it can be added to the errata overview.

Kind regards,
Roel
 
Tomasz Rycerz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply and professional explanation. I fully understood how the GC works, the only problem was the sentence with the objects eligble for GC in Line one. I was afraid that I misunderstood the code, but fortnatelly I was right.

Best Regards,
Tomasz
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellow oca'er. This is an example of a question you can answer by eliminating the wrong answers. A-c all imply you know when gc will run, but in fact you only know when it may run.
 
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
Guillermo Ishi wrote:This is an example of a question you can answer by eliminating the wrong answers. A-c all imply you know when gc will run, but in fact you only know when it may run.

Like the OP mentioned here, he wasn't doubting about the answers but about a statement in the explanation.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. But nevertheless. ..
 
Jo Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I got the following code from Examlab practice exam 4 and the question how many objects are eligible to GC at line 1:




The correct answer apparently is "2". My question is which are these two? Is it correct that these 2 are 1) the original object that c was referencing to before statement c=b and 2) the object that b was referencing (a.aob referencing b in the statement c.aob=a.aob given a.aob=b) ?

Many thanks in advance!
JG
 
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
Hi Jo Gupta,

First of all, a warm welcome to CodeRanch!

Jo Gupta wrote:The correct answer apparently is "2". My question is which are these two? Is it correct that these 2 are 1) the original object that c was referencing to before statement c=b and 2) the object that b was referencing (a.aob referencing b in the statement c.aob=a.aob given a.aob=b) ?

The best approach of solving this kind of questions is draw a diagram of all the objects being created and keeping track of which reference variables refer to which objects. You'll find examples here, here and here.

But if you are in doubt about which objects are eligible for GC (or simply want to confirm your answer), you can always tweak the program a little bit and add a few print statements. Here's the adjusted code snippet of the ExamLab practice question:Output:
a -> A@68dd6317 (aob -> null)
b -> A@1e94b0ca (aob -> null)
c -> A@33e2ad75 (aob -> null)
d -> A@66e9f6ef (aob -> null)
d.aob -> A@a52f9b2 (aob -> null)
--- start ---
a -> A@68dd6317 (aob -> A@1e94b0ca)
b -> A@1e94b0ca (aob -> A@68dd6317)
c -> A@33e2ad75 (aob -> A@1e94b0ca)
c -> A@1e94b0ca (aob -> A@68dd6317)
--- end ---
a -> A@68dd6317 (aob -> A@1e94b0ca)
b -> A@1e94b0ca (aob -> null)
c -> A@1e94b0ca (aob -> null)
d -> A@a52f9b2 (aob -> null)

In section 1 (before --- start ---) you'll find all created objects, in section 3 (after --- end ---) you'll find the final state of all reference variables. So any object wihich is mentioned in section 1 but not in section 3 is eligible for GC

Hope it helps!
Kind regards,
Roel
 
Jo Gupta
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Output:
a -> A@68dd6317 (aob -> null)
b -> A@1e94b0ca (aob -> null)
c -> A@33e2ad75 (aob -> null)
d -> A@66e9f6ef (aob -> null)
d.aob -> A@a52f9b2 (aob -> null)
--- start ---
a -> A@68dd6317 (aob -> A@1e94b0ca)
b -> A@1e94b0ca (aob -> A@68dd6317)
c -> A@33e2ad75 (aob -> A@1e94b0ca)
c -> A@1e94b0ca (aob -> A@68dd6317)
--- end ---
a -> A@68dd6317 (aob -> A@1e94b0ca)
b -> A@1e94b0ca (aob -> null)
c -> A@1e94b0ca (aob -> null)
d -> A@a52f9b2 (aob -> null)

In section 1 (before --- start ---) you'll find all created objects, in section 3 (after --- end ---) you'll find the final state of all reference variables. So any object wihich is mentioned in section 1 but not in section 3 is eligible for GC


Aaaaah - I got it. The two objects are the former objects of C and D. I had missed the former D object which is only a temporary object because it is replaced by "= createA("d.aob");"
Thanks a lot for the guidance!

JG
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic