GarbageCollections
GarbageCollections
Garbage Collection:
—----------------
In C++ programming language Developers are responsible for creating
objects and destroying objects. If Developers are not destroying
objects then we are able to get the following problems.
1. Memory is full with the present application objects, no memory
for the next application objects.
2. No security for the data.
System.gc();
EX:
class A{
A(){
System.out.println("Object Creating.....");
}
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying......");
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
a = null;
System.gc();
}
}
Object Creating.....
Object Destroying......
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying......");
}
}
public class Main {
public static void main(String[] args) {
A a1 = new A();
a1 = null;
A a2 = new A();
a2 = null;
System.gc();
}
}
Object Creating.....
Object Creating.....
Object Destroying......
Object Destroying......
EX:
class A{
A(){
System.out.println("Object Creating.....");
}
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying......");
}
}
public class Main {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
a1 = a2;
System.gc();
}
}
Object Creating.....
Object Creating.....
Object Destroying......
3. Creating Object inside the Methods:
—--------------------------------
If we provide an Object creation statement inside the methods
then JVM will create an object when the respective method is
executed, when method execution is completed , automatically the
object which we have created inside the method is eligible for
Garbage Collection.
EX:
class Test{
psvm(String[] args){
m1();
}
static void m1(){
Student st1 = new Student();
Student st2 = new Student();
}
}
EX:
class A{
A(){
System.out.println("A Class Object Creating....");
}
public void finalize(){
System.out.println("A class Object Destroying.....");
}
}
class B{
void m1(){
A a = new A();
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.m1();
System.gc();
}
}
EX:
EX:
class A{
A(){
System.out.println("Object Creating....");
}
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying.....");
}
}
class B{
A getA(){
A a = new A();
return a;
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
A a1 = b.getA();
System.gc();
}
}
Object Creating....
Island of Isolation:
—----------------
If objects are not having explicit references or the Objects are
having internal references are eligible for GC.
EX:
class Test{
Test t;
psvm(String[] args){
Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
t1.t=t2;
t2.t=t3;
t3.t=t1;
EX:
class A{
A(){
System.out.println("Object Creating....");
}
@Override
protected void finalize() throws Throwable {
System.out.println("Object Destroying.....");
}
}
class B{
A getA(){
A a = new A();
return a;
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.getA();
Runtime runtime = Runtime.getRuntime();
runtime.gc();
}
}
Object Creating....
Object Destroying.....
EX:
import java.util.Date;
class RuntimeDemo{
public static void main(String args[])
{
Runtime r = Runtime.getRuntime();
System.out.println(“total memory of the heap:”+
r.totalMemory());
System.out.println(“free memory of the heap:”+
r.freeMemory());
for(int i=0; i<10000; i++){
Date d = new Date();
d = null;
}
System.out.println(“free memory of the
heap:”+r.freeMemory());
r.gc();
System.out.println(“free memory of the
heap:”+r.freeMemory());
}
}
OUTPUT:
—---
Total memory of the heap: 5177344
Free memory of the heap: 4994920
Free memory of the heap: 4743408
Free memory of the heap: 5049776
finalize() method:
—-----------------
The main intention of finalize() method is
1. To give final intimation about destroying an object.
2. To perform cleanup operations just before destroying an Object.
EX:
class A{
EX:
Status: No Output.
In the above example, the String class object is eligible for Garbage
Collection, so the String class finalize() method will be executed,
not the Main class finalize() method.
EX:
Case 1:
—---
Just before destroying any object, GC always calls the finalize()
method on the Object which is eligible for GC then the corresponding
class finalize() method will be executed.
For Example, if a String object is eligible for GC then the String
class finalize() method is executed but not Test class finalize()
method.
EX:
class Test{
psvm(){
String s = new String(“Durga Software Solutions”);
Test t = new Test();
s = null;
System.gc();
System.out.println(“End of Main”);
}
public void finalize(){
System.out.println(“finalize() method is executed”);
}
}
OUTPUT: End of Main
In the above program the String class finalize() method got executed
which has Empty implementation.
If we replace String object with Test object then Test class
finalize() method will be executed.
EX:
class Test{
psvm(){
Test t = new Test();
t.finalize();
t.finalize();
t=null;
System.gc();
sop(“End of Main.”);
}
public void finalize(){
sop(“finalize() method called”);
}
}
OUTPUT:
finalize() method called.
finalize() method called.
finalize() method called.
End of Main.
In the above program the finalize() method got executed 3 times in
that 2 times explicitly by the programmer and one time by the gc.
Case 3
—----
finalize() method can be call either by the programmer or by the GC.
If the programmer explicitly calls the finalize() method and while
executing the finalize() method if an exception is raised and uncaught
then the program will be terminated abnormally.
class Test{
psvm(){
Test t = new Test();
//t.finalize();-------line(1)
t=null;
System.gc();
sop(“End of main.”);
}
public void finalize(){
sop(“finalize() method called”);
sop(10/0);
}
}
If we do not comment line 1 then the programmer calling finalize()
method explicitly and while executing the finalize() method
ArithmeticException raised which is uncaught hence the program
terminated abnormally.
Case 4:
—----
On any Object, GC calls finalize() method only once.
EX:
class FinalizeDemo{
static FinalizeDemo s;
psvm(String s[])throws Exception{
FinalizeDemo f = new FinalizeDemo();
sop(f.hashCode());
f=null;
System.gc();
Thread.sleep(5000);
sop(s.hashCode());
s=null;
System.gc();
Thread.sleep(5000);
sop(“End of main thread”);
}
public void finalize(){
sop(“finalize method called”);
s = this;
}
}
OUTPUT:
D:\Enum>java FinalizeDemo
4072869
Finalize method called
4072869
End of main method.
Note:
—---
The behaviour of the GC is vendor dependent and varies from JVM to JVM
hence we can’t predict the exact answer for the following.
● What is the algorithm followed by GC.
● Exactly at what time JVM runs GC.
● In which order GC identifies the eligible objects.
● In which order GC destroys the object etc.
● Whether GC destroys all eligible objects or not.
Whenever the program runs with low memory then JVM runs GC, but we
can’t predict exactly at what time.
EX:
class A{
@Override
protected void finalize() throws Throwable {
System.out.println("finalize()-A");
}
void m1(){
System.out.println("m1-A");
}
}
public class Main {
public static void main(String[] args)throws Throwable {
A a = new A();
a.finalize();
a.m1();
}
}
finalize()-A
m1-A
EX:
class A{
@Override
protected void finalize() throws Throwable {
System.out.println("finalize()-A");
}
}
public class Main {
public static void main(String[] args)throws Throwable {
A a = new A();
a = null;
System.gc();
System.gc();
System.gc();
}
}
finalize()-A
EX:
D:\java9>java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseSerialGC Test