Professional Documents
Culture Documents
6)
by www.javabeat.net
1) Which of the following statements are true about Garbage Collection?
1. Applications can only request Garbage Collection to happen, however it is not
guaranteed that the Garbage Collection will always run.
2. A good Garbage Collection implementation should remove both reachable and
non-reachable object references.
3. Garbage Collections provides a way of automated memory management, like freeing
up the un-necessary objects.
4. Garbage Collector Thread always uses Mark and Sweep algorithm for cleaning up
the un-used objects.
2) Which of the following statements are true?
1. It is possible for a Live Thread to access an unreachable object thereby making the
object un-eligible for Garbage Collection.
2. Setting an object reference to null will immediately make the Garbage Collector to
clean-up the object.
3. Calling Sytem.gc() will force the Garbage Collector to happen immediately.
4. Calling Runtime.getRuntime().gc() will only request the Garbage Collector thread to
reclaim the used memory.
3) Which of the following statements are true about the following program?
package gc;
import javax.swing.JButton;
public class Ques01 {
public static void main(String[] args) {
JButton button = new JButton(); // Line A
StringBuilder sb1 = getSB();
getSB(); // Line C
// Line B
3. Even though the object pointed by the reference 'button' is set to null in Line D, the
garbage collector, it is unsure that the Garbage collection reclaims the memory used
by 'button'.
4) Which of the following statements are true about the following program?
package gc;
public class Ques02 {
public static void main(String[] args) {
Object object[] = new Object[1000];
long freeSpace1 = Runtime.getRuntime().freeMemory();
System.out.println(freeSpace1);
for (int i=0; i<object.length; i++){
object[i] = new StringBuilder();
}
long freeSpace2 = Runtime.getRuntime().freeMemory();
System.out.println(freeSpace2);
object = null;
System.gc();
long freeSpace3 = Runtime.getRuntime().freeMemory();
System.out.println(freeSpace3);
}
}
1.
2.
3.
4.
freeSpace1
freeSpace1
freeSpace1
freeSpace1
>
<
<
>
freeSpace2
freeSpace2
freeSpace2
freeSpace2
<= freeSpace3
> freeSpace3
<= freeSpace3
> freeSpace3