aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About GC (Dan's Mock) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About GC (Dan Watch "About GC (Dan New topic
Author

About GC (Dan's Mock)

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Hello all,
The answer for the following code is that "If method m2 causes a reference to be saved for each object that is passed in as an argument, then none of the objects are eligible for garbage collection when line 3 begins."

Is that means the reference of Q (q1) in method m1 pass into m2, assign to a instance reference ?

Thanks in advance!
Jack
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Jack,
In your code example, only the last reference would be saved; so only the last object would not be eligible for gc when the loop completes.
In the following code example, the method m2 saves the reference for each odd numbered instanced of class A. When the loop in method m1 completes, all of the even numbered instances of class A are eligible for garbage collection.
The finalize method of class A prints the name of the instance that is being finalized. When the loop in m1 completes, the System.gc method is invoked to suggest that it might be a good time for the garbage collector to run. There is no guarantee that the gc will run. If it does run, then the finalize methods of the even numbered instanced of class A are likely to be invoked.


Does this make sense to everyone?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52

Is that means the reference of Q (q1) in method m1 pass into m2, assign to a instance reference ?

Well, the answer said :

If method m2 causes a reference to be saved for each object that is passed in as an argument,...

It does not say how it will do it (it can be assigned to instance variable, or it can be assigned to a static variable, or it can even be assigned to a local variable).
However, I think I can see the ambiguity in this question which might confuses Jack. Dan's given answer above only said that the "saving" happened to each object passed to m2. It does not say "how long" it will be saved, particuarly, it is not clear that when line 3 begins, whether those saved references remains unmodified.
So, in a way Jack's interpretation of m2 can be true, because as far as m2 concerns, it always saves the reference of each object passed to it (eventhough only the last one remains saved/unmodified).
Even if m2 saves all the references passed to it without modifying previous saved references, one can also write code where before line 3 begins, another thread is modifying the references (saved in m2) causing the corresponding objects to be eligible for GC.
Perhaps the wording can be made so it is clear that all references passed to and saved in m2 are not modified when line 3 begins?
Does it make sense? Or am I getting to picky?
P.S. : I think this is a good question of Dan, for it is not only testing knowledge about GC, but also test one's ability to draw the correct conclusion from a given statement (if method m2...) rather than merely reading the code.
[ January 22, 2004: Message edited by: David Hadiprijanto ]
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
makes lots of sense...great example


"Did anyone understand what I have just explained? ... because I did not!"
Brett Delia
Greenhorn

Joined: Oct 27, 2003
Posts: 6
That did help, thanks.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865

The correct answer is e, Indeterminate. I think the correct answer is consistent with you statements.
[ January 22, 2004: Message edited by: Dan Chisholm ]
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Oops, after looking up at the whole question, answer, and remarks, of the discussed question, it is apparent that my comments above about re-phrasing was not nescessary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About GC (Dan's Mock)