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.
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
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.
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.
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
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).