You are on page 1of 3

GarbageCollection (SCJP 1.

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

button = null; // Line D


}
static StringBuilder getSB(){
return new StringBuilder("");
}
}
1. The button object 'button' will become a candidate for garbage collection as soon
the execution of Line A ends since the object is not used elsewhere.
2. The newly created StringBuilder object as a result of method call in Line C becomes
eligible for garbage collection.

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

5) What will be the output of the following program?


package gc;
public class Ques03 {
public static void main(String[] args) {
MyReference ref1 = new MyReference();
MyReference ref2 = new MyReference();
ref1.reference = ref2;
ref2.reference = ref1;
ref1 = null;
ref2 = null;
}
}
class MyReference {

MyReference reference = new MyReference();


}
1. Both the references 'ref1' and 'ref2', after being set to null will soon become eligible
for garbage Collection.
2. The references 'ref1' and 'ref2' won't be eligible for Garbage Collection even after
making them to point 'null', since the instance variable 'reference' is not set to null.
3. The program will end up in StackOverflowError because of circular references.
SCJP group : http://tech.groups.yahoo.com/group/JavaBeat_SCJP/
SCJP Forums : http://forums.javabeat.net/index.php?board=2.0
Web URL : http://www.javabeat.net/cert/scjp-6-0/
Send your feedback to : admin@javabeat.net