As
of Summary: Garbage
collector revolves around making sure that the heap has as
much
free space as possible. Objects are created in the heap. When
JVM(Java Virtual Machine) senses
that memory is running low,
at that time it calls garbage collector to remove/delete the unused
objects that are not needed any more from memory(heap)...
Explicitly
Makes Objects Eligible for Collection
(a). Nulling
a Reference:
- An object becomes eligible for garbage collection when there are no more reachable references to it. To remove a reference to an object is to set the reference variable that refers to the object to null.
Program
code :
public class RemoveReference {
public static void main(String [] args) {
StringBuffer sb = new StringBuffer("Reference");
System.out.println(sb);
// Here the StringBuffer object is not eligible for collection
sb = null;
// Now the StringBuffer object is eligible for collection
}
}
(b).
Reassigning
a Reference Variable:
- We can also decouple a reference variable from an object by setting the reference variable to refer to another object.
- The objects created in the method are eligible for garbage collection ,once the method has returned.
- If an object is returned from the method, its reference might be assigned to a reference variable in the method that called it; hence, it will not be eligible for collection
Program
Code:
import java.util.Date;
class ReassignReference {
public static void main(String [] args) {
Date d = getDate(); // Reference return from the method
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}
public static Date getDate() {
Date d2 = new Date();
StringBuffer now = new StringBuffer(d2.toString());
// object eligible to garbage collector even we don't set the object as null explicitly.
System.out.println(now);
return d2; // Reference Return , not eligible for garbage collector
}
}
class ReassignReference {
public static void main(String [] args) {
Date d = getDate(); // Reference return from the method
StringBuffer s1 = new StringBuffer("hello");
StringBuffer s2 = new StringBuffer("goodbye");
System.out.println(s1);
// At this point the StringBuffer "hello" is not eligible
s1 = s2; // Redirects s1 to refer to the "goodbye" object
// Now the StringBuffer "hello" is eligible for collection
}
public static Date getDate() {
Date d2 = new Date();
StringBuffer now = new StringBuffer(d2.toString());
// object eligible to garbage collector even we don't set the object as null explicitly.
System.out.println(now);
return d2; // Reference Return , not eligible for garbage collector
}
}
(c) .Isolating a Reference
1.There is another way in which objects
can become eligible for garbage collection,even if they still have
valid references! We call this scenario "islands of
isolation."
Program Code :
public class Island {
Island i;
public static void main(String [] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
public class Island {
Island i;
public static void main(String [] args) {
Island i2 = new Island();
Island i3 = new Island();
Island i4 = new Island();
i2.i = i3; // i2 refers to i3
i3.i = i4; // i3 refers to i4
i4.i = i2; // i4 refers to i2
i2 = null;
i3 = null;
i4 = null;
// do complicated, memory intensive stuff
}
}
When
the code reaches //
do complicated,
the three Island objects (previously known as i2, i3, and i4) have
instance variables so that they refer to each other, but their links
to the outside world (i2, i3, and i4) have been null. These three
objects are eligible for garbage collection.
(d)
Forcing
Garbage Collection
- Garbage collection cannot be forced by a programmer.
- However, Java provides some methods that allow you to request that the JVM perform garbage collection.
- We can call garbage collector using Runtime java classes.
Ex: Runtime.getRuntime().gc();
Ex: System.gc();
Great article about making a object eligible for Garbage Collection....
ReplyDelete