• 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
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Garbage Collector

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am new to java and I am getting confused in one statement on Garbage collector. Please help me with this.


So, I wanted to know that, when we did a2 = null on Line 1, so I just wanted to confirm that a2 object becomes eligible for Garbage collection on the same line or when the code flow goes to Line 2. Please help me with this, it is creating a confusion in my mind.
Thank you.
 
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not something you need to know to program Java®, only to pass a cert exam. If you read something elsewhere, please tell us where it was.
There is no such thing as an object a2, but there is a reference a2 which points to an object. The object behind that reference becomes eligible for GC when line 9 (what you called // line 1) completes. What difference is there between the timings of the two lines you showed?
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie for the reply. Actually I am giving Java 8 OCA exam this week and while practicing the mock test exam given in Mala Gupta OCA 8 book, I came across this question. That is why I wanted to know that if the object referred by a2 will become eligible for GC at Line 1 or when code reaches Line 2. I know in real world programming it might not be of that significance, however from Certification point of view, I have to prepare for every single aspect.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

This was the actual question and answer was Option: - d,e

So after visiting this question, this thing strikes my mind that what if the examiner asked specifically that object referred by a2 will become eligible for GC at Line number 1 or not ?

And I am beginner with java, so please ignore if I made any wrong statement,
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is different from what you posted at first. It is most likely that no GC occurs at all; there is not enough heap use to make it worthwhile. You do not therefore know whether anything will be GC‑ed on // line 1 (d) and the objects behind a1 and a2 are unreachable by // line 2 (e).
Since the behaviour of the GC is not predictable, none of a, b, and c can be correct; they are all mutually incompatible with d.

Moving to our OCA forum.
If you really are a beginner, I suggest you delay the exam. You should be getting 70%+ regularly on the mock tests before sitting the real thing.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats true, even if we call GC it is not compulsory that GC will run. I was asking in a hypothetical way, that suppose GC runs, then object referred by a2 will become eligible for GC at the same line (// line 1) or when the compiler reaches next line.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is what d and e mean; you don't know whether the GC will run at all, but if it does run it will delete the object behind a2 only. As I said, the object behind a2 becomes eligible for GC when // line 1 completes, i.e. the end of  // line 1.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie for explaining with such a patience and clearly. I got it now. Once again thank you so much.
 
Campbell Ritchie
Marshal
Posts: 73766
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahil Kairon wrote:. . . I got it now. . . .

Well done
 
Bartender
Posts: 1064
33
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additionally, the words "on line X" are problematic.
I think the better quality Exam Preparation materials say "after line X" or "after line X" completes.
The term "on line X" is fine for referring to syntax errors or scope, but not for runtime analysis.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic