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

Garbage collect from javaprepare.com Exam

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Which of the following are correct related to Garbage Collection. Select all correct answers.
a. It is posible for a program to free memory at a given time.
b. Garbage Collection feature of Java ensures that the program never runs out of memory.
c. It is possible for a program to make an object available for Garbage Collection.
d. The fialize method of an object is invoked before garbage collection is performed on the object.
The answer given is b,c. But I choose b, c and d. B'case if the finalize method exist then it will be invoked right???
help me pls.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Can anyone pls. explain me??
 
satish
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think garbage collection ensures that your program never runs out of memory. You can code in such a way that you leave references to objects but don't use those objects anymore. Garbage collection can act on only objects that do not have any references.
Regarding the finalize method...to my knowledge, finalize method will be called (if it exists) before removing the object. However if you resurrect your object reference in finalize method then the finalize method will not be called again when u remove the reference to the object later. So, finalize method is called only once for an object.
I am really not sure if I would not select answer d. Inspite of the above scenario, my instinct would be to select d. I am also interested in knowing what others think about this.
satish
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
According to Exam Cram by Bill Brogden ,
"If your class definition declares an instance method that overrides finalize() method, the JVM will run the method after it has decided that an object is eligible for reclamation, but before reclaiming the memory used by the object"
The above statement sounds like it happens only when the finalize() method is overridden.
 
Anil Kollur
Ranch Hand
Posts: 101
Java Linux Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, answers c and d r correct, Garbage Collection of java will not ensure that, program will never run out of memory. And finalize method will be invoked before GCing the object.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hi,
So you are all accepting that ans 'd' is correct. I am pretty sure that ans 'b' is correct. B'cas I read somewhere in this discusion that JVM ensures that the program never runs out of memory.
The program should not go out of memory that is why the automatic garbage collection runs in the backround with low-priority. But if you keep on creating objects and make those objects available for GC. If GC never gets a chance to run at that time your memory will keep on gets occupied, at some stage the GC will identify this situation and the GC will start run and our program will not run at that time.
The above concept I read somewhere not 100% sure. Please any one can help us. Planning to take exam on coming weekend.
Pls, pls. help us.
Thanks in advance.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surya,
A program can indeed run out of memory. Though the garbage collector will be eventually run by the JVM, there is no guarantee your program will not get impoverished of memory.
Perhaps that's why OutOfMemoryException exists
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
I believe the answer has been corrected on the website. The correct answer is c,d.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic