wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt about garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt about garbage collection" Watch "doubt about garbage collection" New topic
Author

doubt about garbage collection

p anish
Ranch Hand

Joined: Feb 26, 2005
Posts: 32
This is question from dan chisholm

class Q {
private int id;
protected void finalize() {System.out.print(id);}
public Q(int i) {id = i;}
}
class R {
public static void main(String[] args) {
Q q1 = null;
for (int i = 0; i < 10; i++) {q1 = new Q(i);} // 1
System.gc(); // 2
}}

When the processing of line 2 begins, how many objects of type Q that were created at line 1 have become eligible for garbage collection?

The answer given is 9 with explanation:
With each pass through the loop, q1 references a new object, and the old object becomes eligible for garbage collection. When the processing of line 2 begins, the last object referenced by q1 is not eligible for garbage collection.

my doubt is ,Is there a possibility of runing garbage collector before calling System.gc() in line number 2, so that the answer may be less than 9.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Yeah there may be a possibility, u may not know when garbage collector starts running
But i hope the answer is not creating confusion to u. The question clearly asks for the number of objects of type Q eligible for garbage collection when line 2 starts processing.
p anish
Ranch Hand

Joined: Feb 26, 2005
Posts: 32
Yes there is confusion bcse the choice also contains
indeterminate
I think indeterminate will be the good answer what do u say??
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You may be technically correct, but you will be marked wrong unless you select 9.

In a program that small, there is no way the gc will be run right after the program has started unless you call System.gc(). THat would be very inefficient and the jvm is designed to be efficient.


Mike Gershman
SCJP 1.4, SCWCD in process
p anish
Ranch Hand

Joined: Feb 26, 2005
Posts: 32
thank u Mike.
But still i am not clear.
What will happen in a scenario if lot of other java pgms also running and Virtual machine cannot allocate no more objects?
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by anish pathadan:
thank u Mike.
But still i am not clear.
What will happen in a scenario if lot of other java pgms also running and Virtual machine cannot allocate no more objects?


You are missing the point. The key word here is eligible. It is NOT asking how many objects are yet to be collected, or how many have been collected.
[ February 27, 2005: Message edited by: Alton Hernandez ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
And can you please write in correct English. Would make it a lot easier for people to actually understand your question...


42
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Jeroen Wenting:
And can you please write in correct English. Would make it a lot easier for people to actually understand your question...


Hey Jeroen,

Are you talking about grammar or those annoying abbreviations?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I think that it is important that the people posting here think about other people with very limited English. Most of the clever abbreviations are phonetic and very difficult for someone without good spoken English. It only takes a few more seconds to spell out all the words.

As for grammar, it's hard to tell whether the posters are being sloppy or are doing the best they can, so I, for one, won't criticise grammar.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
To answer the earlier question, we are guaranteed the the jvm will run the gc if there is a memory shortage. Since we are talking about a huge virtual address space, this would require either a really massive program or a program written (accidently or deliberately) to use up all of memory.

If there are a lot of unrelated programs running, most operating systems give each program its own address space, so that does not use up all of memory.
p anish
Ranch Hand

Joined: Feb 26, 2005
Posts: 32
Thank you all
Sachin Gharat
Greenhorn

Joined: Mar 02, 2005
Posts: 5
Hi
The question clearly states 'objects eligible for garbage' collection,
so at the end of pass 10, q will have reference Q(9), which however means objects Q(0..8) will be "eligible for garbage collection,i.e 9 objects.

Garbage collection is done by running a low priority thread(by the JVM). When we call System.gc or Runtime.gc we ask that thread to delibrately collect the garbage at that time, but this doesn't guarantee that the garbage collection will occur, because some other thread with higher priority may stop this thread for a while.

So i guess running the garbage collector before line 2 is not an issue as it cannot be forced.
Neither does it force the jvm to run the garbage collector, so in conclusion neither does it change the answer to the question.

Cheers
Sachin
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt about garbage collection
 
Similar Threads
garbage collection
gc question
Garbage collection dan's question
Garbage Collection doubt
Dan's GC question