File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About GC (Dan's Mock)

 
Jack Lau
Ranch Hand
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Hadiprijanto
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makes lots of sense...great example
 
Brett Delia
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did help, thanks.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic