• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

doubt about garbage collection

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes there is confusion bcse the choice also contains
indeterminate
I think indeterminate will be the good answer what do u say??
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
p anish
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And can you please write in correct English. Would make it a lot easier for people to actually understand your question...
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Always look on the bright side of life. At least this ad is really tiny:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic