Hi All,
In this post, I am going to show you Two examples
1 - Example program for limiting objects creation using factory method style (Singleton).
2 - Usage of System.gc() using the above example.
1) Limiting Object's creation: (requirement is creation of objects to a class is 7 only)
ObjectFactory7.java:
package com.nagarjuna.core.singletons;
public class ObjectFactory7 {
private static int objcount = 0;
private ObjectFactory7() {
}
public static ObjectFactory7 getInsance() throws ObjectCreationException {
ObjectFactory7 obj = null;
//
if (objcount < 7) {
obj = new ObjectFactory7();
objcount++;
} else {
throw new ObjectCreationException();
}
return obj;
}
@Override
protected void finalize() throws Throwable {// decrease the count of objcount for
// Garbage Collection of each
// object
objcount--;
}
public static void showNumberOfObjects() {
System.out.println("no of objects created from this factory are :"+ objcount);
}
}//end class
ObjectCreationException.java:
package com.nagarjuna.core.singletons;
public class ObjectCreationException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Object creation exception occured, Creation of objects is limited to sevev (7) only..");
}
}//end class
ObjCreationTest.java
package com.nagarjuna.core.singletons;
public class ObjCreationTest {
public static void main(String[] args) {
ObjectFactory7 o1, o2, o3, o4, o5, o6, o7, o8;
try {
ObjectFactory7.showNumberOfObjects();
//
o1 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o2 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o3 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o4 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o5 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o6 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o7 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
System.out.println("-----------------------------------------------");
//
o1 = null;
ObjectFactory7.showNumberOfObjects();
//
o8 = ObjectFactory7.getInsance();//trying to create 8th object
ObjectFactory7.showNumberOfObjects();
} catch (Exception e) {
e.printStackTrace();
}
}
}//end class
If we try to run the above code we get an exception at 8th object creation code. we made o1 unreachable (null) and must be garbage collected.
output:
no of objects created from this factory are :0
no of objects created from this factory are :1
no of objects created from this factory are :2
no of objects created from this factory are :3
no of objects created from this factory are :4
no of objects created from this factory are :5
no of objects created from this factory are :6
no of objects created from this factory are :7
-----------------------------------------------
no of objects created from this factory are :7
Object creation exception occured, Creation of objects is limited to seven (7) only..
___________________________________________________________________________________________
The above code is an example for both Singleton and Limit Objects creation.
2) Usage of System.gc() :
ObjCreationTest.java:
package com.nagarjuna.core.singletons;
public class ObjCreationTest {
public static void main(String[] args) {
ObjectFactory7 o1, o2, o3, o4, o5, o6, o7, o8;
try {
ObjectFactory7.showNumberOfObjects();
//
o1 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o2 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o3 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o4 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o5 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o6 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o7 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
System.out.println("-----------------------------------------------");
//
o1 = null;
// System.gc();
Thread.sleep(500);
ObjectFactory7.showNumberOfObjects();
//
o8 = ObjectFactory7.getInsance();//trying to create 8th object
ObjectFactory7.showNumberOfObjects();
} catch (Exception e) {
e.printStackTrace();
}
}
}//end class
When we try to run the above code, exception will be raised.
If we observe the above code in ObjectFactory7.java, It has finalize() method and by using this method we can decrease the count of objcount.
But we will get an exception, It means Garbage Collector is not guaranteed for garbage collection, and also we can't force the GC to collect the unreachable object.
But we can ask the GC to collect the unreachable object to be garbage collected by adding code System.gc() after below statement.
o1 = null;
Now try to run the above code with out Thread.sleep(500), Still we get the same exception. It means Calling System.gc() is also useless.
Since GC is a daemon service with the thread priority of 5 and runs in the background, But when we try to execute System.gc(), the GC will try to remove the memory of the unreachable object after some time ( must give some amount of time like Thread.sleep(10) or (1) also). The GC will work perfectly.
Now uncomment System.gc(), also keep Thread.sleep(500) and then run the code.
You won't get the exception and the count also decreases and the new object creation code also will be executed successfully.
Try it once.
Please give your feedback at nagarjuna.lingala@gmail.com
In this post, I am going to show you Two examples
1 - Example program for limiting objects creation using factory method style (Singleton).
2 - Usage of System.gc() using the above example.
1) Limiting Object's creation: (requirement is creation of objects to a class is 7 only)
ObjectFactory7.java:
package com.nagarjuna.core.singletons;
public class ObjectFactory7 {
private static int objcount = 0;
private ObjectFactory7() {
}
public static ObjectFactory7 getInsance() throws ObjectCreationException {
ObjectFactory7 obj = null;
//
if (objcount < 7) {
obj = new ObjectFactory7();
objcount++;
} else {
throw new ObjectCreationException();
}
return obj;
}
@Override
protected void finalize() throws Throwable {// decrease the count of objcount for
// Garbage Collection of each
// object
objcount--;
}
public static void showNumberOfObjects() {
System.out.println("no of objects created from this factory are :"+ objcount);
}
}//end class
ObjectCreationException.java:
package com.nagarjuna.core.singletons;
public class ObjectCreationException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Object creation exception occured, Creation of objects is limited to sevev (7) only..");
}
}//end class
ObjCreationTest.java
package com.nagarjuna.core.singletons;
public class ObjCreationTest {
public static void main(String[] args) {
ObjectFactory7 o1, o2, o3, o4, o5, o6, o7, o8;
try {
ObjectFactory7.showNumberOfObjects();
//
o1 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o2 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o3 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o4 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o5 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o6 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o7 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
System.out.println("-----------------------------------------------");
//
o1 = null;
ObjectFactory7.showNumberOfObjects();
//
o8 = ObjectFactory7.getInsance();//trying to create 8th object
ObjectFactory7.showNumberOfObjects();
} catch (Exception e) {
e.printStackTrace();
}
}
}//end class
If we try to run the above code we get an exception at 8th object creation code. we made o1 unreachable (null) and must be garbage collected.
output:
no of objects created from this factory are :0
no of objects created from this factory are :1
no of objects created from this factory are :2
no of objects created from this factory are :3
no of objects created from this factory are :4
no of objects created from this factory are :5
no of objects created from this factory are :6
no of objects created from this factory are :7
-----------------------------------------------
no of objects created from this factory are :7
Object creation exception occured, Creation of objects is limited to seven (7) only..
___________________________________________________________________________________________
The above code is an example for both Singleton and Limit Objects creation.
2) Usage of System.gc() :
ObjCreationTest.java:
package com.nagarjuna.core.singletons;
public class ObjCreationTest {
public static void main(String[] args) {
ObjectFactory7 o1, o2, o3, o4, o5, o6, o7, o8;
try {
ObjectFactory7.showNumberOfObjects();
//
o1 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o2 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o3 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o4 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o5 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o6 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
//
o7 = ObjectFactory7.getInsance();
ObjectFactory7.showNumberOfObjects();
System.out.println("-----------------------------------------------");
//
o1 = null;
// System.gc();
Thread.sleep(500);
ObjectFactory7.showNumberOfObjects();
//
o8 = ObjectFactory7.getInsance();//trying to create 8th object
ObjectFactory7.showNumberOfObjects();
} catch (Exception e) {
e.printStackTrace();
}
}
}//end class
When we try to run the above code, exception will be raised.
If we observe the above code in ObjectFactory7.java, It has finalize() method and by using this method we can decrease the count of objcount.
But we will get an exception, It means Garbage Collector is not guaranteed for garbage collection, and also we can't force the GC to collect the unreachable object.
But we can ask the GC to collect the unreachable object to be garbage collected by adding code System.gc() after below statement.
o1 = null;
Now try to run the above code with out Thread.sleep(500), Still we get the same exception. It means Calling System.gc() is also useless.
Since GC is a daemon service with the thread priority of 5 and runs in the background, But when we try to execute System.gc(), the GC will try to remove the memory of the unreachable object after some time ( must give some amount of time like Thread.sleep(10) or (1) also). The GC will work perfectly.
Now uncomment System.gc(), also keep Thread.sleep(500) and then run the code.
You won't get the exception and the count also decreases and the new object creation code also will be executed successfully.
Try it once.
Please give your feedback at nagarjuna.lingala@gmail.com
No comments:
Post a Comment