• No results found

What allows the programmer to destroy an object x? A x.delete()

Notes on Assertions

81. What allows the programmer to destroy an object x? A x.delete()

B. x.finalize()

C. Runtime.getRuntime().gc()

D. explicitly setting the object's reference to null E. ensuring there are no references to the object

F. Only the garbage collection system can destroy an object. Answer: F

Explanation:

A is wrong. I found 4 delete() methods in all of the Java class structure. They are:

(1) delete() - Method in class java.io.File

Deletes the file or directory denoted by this abstract pathname. (2) delete(int, int) - Method in class java.lang.StringBuffer

Removes the characters in a substring of this StringBuffer.

(3) delete(int, int) - Method in interface javax.accessibility.AccessibleEditableText Deletes the text between two indices

(4) delete(int, int) - Method in class

javax.swing.text.JTextComponent.AccessibleJTextComponent Deletes the text between two indices

None of these destroy the object to which they belong.

B is wrong. I found 19 finalize() methods. The most interesting, from this question’s point

of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more

references to the object. This method does not destroy the object to which it belongs.

C is wrong. But it is interesting. The Runtime class has many methods, two of which are:

getRuntime() - Returns the runtime object associated with the current Java application gc() - Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.

Interesting as this is, it doesn’t destroy the object.

D is wrong. Setting the object’s reference to null does just that. It doesn’t set the object

to null. Also there might be other references to the object hanging around.

E is wrong. If there are no references to an object your program cannot access the

object, but the object might still exist on the heap.

F is correct. When an object is no longer referenced, it may be reclaimed by the garbage

collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded.

Source code file: Exam Objective/s:

82. Which statement is true?

A. Memory is reclaimed by calling Runtime.gc().

B. Objects are not collected if they are accessible from live threads. C. Objects that have finalize() methods are never garbage collected.

D. Objects that have finalize() methods always have their finalize() methods called before the program ends.

E. An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.

Answer: B

Explanation:

A is wrong. Runtime.gc() asks the garbage collector to run, but the garbage collector

never makes any guarantees about when it will run or what unreachable objects it will free from memory.

B is correct. If an object can be accessed from a live thread, it can’t be garbage

collected.

C is wrong. The finalize() method is called by the garbage collector when the garbage

collector sees that the object cannot be referenced. Java provides this mechanism for you so that you can run some code just before your object is deleted by the garbage collector. This is a good idea because it enables you to close any resources opened by your object before your object is deleted. However, any code that you put into your class’s overridden finalize() method is not guaranteed to run. The finalize() method for any given object might run, but you can’t count on it, so don’t put any essential code into your finalize() method. There is another caveat, it is possible, through poor programming of the finalize() method to cause an object to never be garbage collected (for more information, research memory leaks). This last point is not of concern to us here in this question.

D is wrong. If this were the case then the garbage collector would actively hang onto

objects until a program finishes – this goes against the purpose of the garbage collector.

E is wrong. The garbage collector runs immediately the system is out of memory before

an OutOfMemoryException is thrown by the JVM. Source code file:

Exam Objective/s:

83. Which statement is true?

A. Programs will not run out of memory.

B. Objects that will never again be used are eligible for garbage collection. C. Objects that are referred to by other objects will never be garbage collected. D. Objects that can be reached from a live thread will never be garbage collected.

E. Objects are garbage collected immediately after the system recognizes they are eligible. Answer: D

Explanation:

A is wrong. Even though Java applications can run out of memory there another answer

supplied that is more right.

B is wrong. “Never again be used” does not mean that there are no more references to

the object.

C is wrong. See the note above on Islands of Isolation (An object is eligible for garbage

collection when no live thread can access it - even though there might be references to it).

D is correct.

E is wrong. The garbage collector guarantees nothing. Source code file:

Exam Objective/s:

84. Click the Exhibit button.

Related documents