• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

What does it mean to be eligible for gc?

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to understand what it means to be eligible for garbage collection. (This is almost a trick question.)

How many objects are eligible for garbage collection
a. at line 1?
b. at line 2?
c. at line 3?
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marlene
Let me try
Perdict only
Assume the new object action which creates only one object (concept in c++) (not heap and stack concept)
How many objects are eligible for garbage collection
a. at line 1?
2
Because the three object are point to same object and the other two will be collected.
b. at line 2?
1
After finish the method m(),one object is collected
c. at line 3?
0
I hope that I can predict correctly.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene -
Ok I'll bite again, this seems to be straightforward, not tricky...
What do you think the answer is and why..
(btw I disagree with siu's answer)
-Bert
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think at lines 1, 2 and 3, the number of objects eligible for garbage collection could be 0, 1, 2 or 3.
The garbage collection thread could preempt the main thread and run prior to the execution of lines 1 and 2. The garbage collector could choose to reclaim none, some or all of the unreachable objects.
If the garbage collector reclaims an object, that object is no longer eligible for garbage collection.
Does that make sense to you? What do you think?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my amateurish explanation:
three objects get created in a method, and at line one the three references are set to null. so i'd say that the objects to which they refered are eligible. there are no external references to these local-method-scoped objects. they are not referenced by an external collection, and references to them are not returned by the method. i can't see anything that would keep then ineligible. but these are just the guesses of a humble ranch hand!...
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene - Peter is basically right, check out this link and then see what you think...
https://coderanch.com/t/240324/java-programmer-SCJP/certification/One-more-time
Let me know !
-Bert
[ April 30, 2003: Message edited by: Bert Bates ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'll take a stab at this, and I'm going to use my line numbering:
If GC was invoked by the JVM after execution of line 7 (Marlene's line 1), 3 objects are eligible for G.C. This is because the assignment operators on line 6 are evaluated from right to left (ie. o1 = o2 = o3 = null; is equivalent to o1 = (o2 = (o3 = null)); ) Therefore the objects that were previously referred to by o1, o2, and o3 are no longer referred to by a reference in a live thread, and therefore eligible for G.C.
If GC was invoked by the JVM no earlier than after execution of line 11, 3 objects are still eligilbe for G.C. This is because the int on line 7 is a method local primitive variable, and therefore created on the stack, not on the heap. The three Objects created in m() are eligible for GC by my explaination above, but even if line 6 was not present, they would be eligible since the reference variables o1, o2, and o3 are also method local, meaning they are also created on the stack, and removed once execution is returned to t() from m(). That would mean that the Objects are no longer referenced by any variables in a live thread, and eligible for GC.
If GC was invoked by the JVM no earlier than after execution of line 13, the same 3 objects are eligible for GC. Because System.gc() is only a suggestion, there is no guarantee that GC will be invoked by the time execution of line 13 occurs.
In summary, the three Object()s created in m() are eligible for GC anytime after line 6 is executed.
[ April 30, 2003: Message edited by: David Willis ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Object o1 = new Object();
2. o1 = null;
3. int i = 1;
The main thread executes lines 1 and 2.
The garbage collector preempts the main thread.
The garbage collector reclaims the object.
The main thread resumes execution at line 3.
At line 3, the object is not eligible for garbage collection, because it has already been reclaimed. There is no object.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene - if you're talking about the certification test, then the question about "being eligible for gc" would only be asked at one point - so I would say 3 objects also. You don't really know WHEN gc will run, so once an object is made eligible for gc, you continue to say that it is "eligible" for gc - you never assume it has been collected.
David - I agree with your analysis.
Now, for extra credit, can you explain why you didn't even consider x, y, or z for garbage collection?
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene, whilst I applaud your efforts to get an in-depth understanding of many areas of Java, I think that a "HIGHLY DETAILED NOT NEEDED FOR EXAM" warning is needed as most people may become confused or alarmed. For the exam, they will need to know a few key facts, eg.
1. gc normally runs in a low priority daemon thread.
2. gc cannot be forced by the programmer.
3. Only gc can destroy objects.
4. gc will call finalizer once before destroying objects.
5. Objects are eligible for gc if not reachable from any live thread.
On this basis, and for the exam, there would be three objects being eligible for gc at line 1.
[ April 30, 2003: Message edited by: Roger Chung-Wee ]
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene, that's why I try to state in my analysis, "If GC is invoked no earlier than...". It's pointless to try to determine how many objects are eligible for GC if you can't specify when GC preempts the normal execution of a program.
Dorothy, the reason x, y and z are not eligible for GC is, because they are method local variables, they are allocated on the stack. Since x, y, and z are primitives, the bit patterns are stored in the locations allocated on the stack, rather than a reference to a location in the heap for an object. They are eliminated as soon as the methods they are instanatiated are popped of the stack.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Roger, Marlene. Just think about it, with your answer you just dumped another 2 points down the chute. Think simple, the answer for SCJP2 is 3 at line 1.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you eligible to vote? Yes. Why? Because you are a citizen and you have registered. Now that your registration has expired, are you eligible to vote? No.
The garbage collector thread and the main thread are asynchronous.
(post script: I should have said - Now that you have expired...)
[ April 30, 2003: Message edited by: Marlene Miller ]
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps another way to study how GC works is rather than trying to determine how many objects are eligible for GC at a particular line without knowing whether GC was invoked prior to that line, ask the question of how many objects will have been cumulatively collected by the GC thread at a particular line of a program. This forces a person to examine when objects become available to be GC'ed without having to worry about how many times GC was invoked prior to the current line. I don't think the exam has questions phrased like this, though.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sui, Bert, Peter, David, Dorothy, Roger and Barry.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the real exam you will often be asked
'At line x how many objects are eligible for GC'
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For considering how many objects are eligible for g.c.ion you do not have to worry about if the g.c.or has already run. It is necesary only to consider if the objects are reachable from a live thread. It is likely that the term eligible for g.c.ion was chosen to imply the dismisal of the ocurrances of g. collections at the point of the program being considered.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bert and Jose. We are to assume the garbage collector does not run (even though this question is testing knowledge of garbage collection).
Now, suppose I had a fancy IDE that showed me the reachable objects and the unreachable objects as I stepped through my program.
Would anyone be willing to confirm that at lines 1, 2 and 3 in my example program, it is possible that I would see 0, 1, 2 or 3 unreachable objects of class Object? (not counting other objects created by the VM)
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene -
If your fancy IDE ran it would say that as of line 1 there were three unreachable objects of class Object. No new objects are created subsequent to line 1 so your IDE wouldn't have anything new to say when it reached lines 2 or 3.
Did that other thread help?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did that other thread help?


Bert, thank you for the link. I think I understand the concept of root references.
However, that discussion does not address the issue that concerned me, namely the effect of the garbage collector thread when counting eligible objects.
Some people think the number of eligible objects at line 1 is 3. But I think if the garbage collector thread preempts the main thread before line 1 is executed, some of those unreachable objects might be reclaimed. If they no longer exist, they cannot be eligible.
I guess I have to accept for now that some people do not want to think about the effect of the garbage collector thread.
[ May 01, 2003: Message edited by: Marlene Miller ]
 
David Willis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taking into account your desire to not know whether or not GC has already preempted the main thread by the time it reaches lines 1, 2 or 3, then the answer to your questions are:
The amount of objects available for garbage collection is [0,3] at lines 1, 2, or 3.
But there is absolutely no way for you to know the precise number of objects, and it really isn't important, both from the perspective of taking the exam or from the practical perspective actually using Java to solve problems programatically. The whole point of these questions is to determine whether an exam taker understands when objects first become eligible for garbage collection. The answer to that question is: 3 objects become eligible after the execution of the line preceding line 1 ( o1 = o2 = o3 = null ) . They will be collected the next time GC preempts the main thread anytime after the reference variables are assigned null.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you David.
It would have been much better if I had never asked my question. It is a very silly question. I will try to keep my questions appropriate to the purpose.
But if you should ever work through the 15 questions on garbage collection in Dan Chisholm�s Study Guide and try to understand what might or might not happen after System.gc is called so that you can predict whether and when Arnold will be back, then you might see why this happened.
reply
    Bookmark Topic Watch Topic
  • New Topic