corejava interview questions part 6
Q - The purpose of garbage collection in Java is to perform
all necessary cleanup and the memory
occupied by objects that are no longer needed will always be
reclaimed by the garbage collector:
True or False? If false, explain why.
A - False. The sole purpose of garbage collection is to
reclaim memory occupied by objects that are no longer needed, and it has no
other purpose relative to necessary cleanup. An object becomes eligible for
garbage collection when there are no more references to that object. However,
just because an object is eligible for garbage collection doesn't mean that it
will be reclaimed. The garbage collector runs in a low-priority thread, and may
not run at all unless a memory shortage is detected.
Q - Before the garbage collector reclaims the memory occupied
by an object, it always calls the
object's _________ method. (Provide the name of the method.)
Explain why this method is one of
the methods in all new classes that you define.
A - Before the garbage collector reclaims the memory
occupied by an object, it calls the object's finalizemethod. The finalize
method is a member of the Object class. Since all classes inherit from the
Object class, your classes also contain the default finalize method.
Q - What must you do to make effective use of the finalize
method? Explain why you might want to
do this.
A - In order to make use of the finalize method, you must
override it, providing the code that you want to have executed before the
memory is reclaimed.
Q - You can always be confident that the finalize method
will be promptly executed to perform
necessary cleanup in a Java program when an object becomes
eligible for garbage collection: True
or False? If false, explain why.
A - False. Although you can be confident that the finalize
method will be called before the garbage collector reclaims the memory occupied
by a particular object, you cannot be certain when, or if that memory will be
reclaimed. There is no guarantee that the memory will be reclaimed by the
garbage collector during the execution of your program.
Q - Provide a code fragment illustrating the method call
that you can make to ask the garbage
collector to run. This guarantees that garbage collection
will take place: True or False? If false,
explain why.
A - False. Campione and Walrath indicate that you can ask
the garbage collector to run at any time by calling the method shown below.
They further point out, however, that making the request does not guarantee
that your objects will be reclaimed through garbage collection. System.gc();
0 comments:
Post a Comment