Garbage Collection.

In this article, Garbage Collection is explained in detail with examples.
1.It is an intelligent piece of an algorithm, provided by "JVM", which performs cleaning operations in the memory of java program dynamically when a java program is running for a long period of time.
2.This algorithm checks for unused memory in JVM RAM,and destroys them automatically.
3.In doing so,memory is saved by JVM and RAM becomes free, hence performance of the program increases.
4.This algorithm runs in the background of a program by JVM in memory.
5.This concept is same as that of destructors in C++.
Note
1.This automatic process can also be done by the user by calling the following method.
public void gc();
This method is available in "Runtime" class.It is available in "java.lang" package. (From topic Garbage Collection)
2.This class object is created using following method
Runtime getRuntime();
other methods are
long freeMemory();
long totalMemory();
3.when ever GC is done automatically by JVM or manually by user,unused memory is destroyed by calling the following method internally.
protected void finalize();
*This method is available to all classes from object class(because it is a super class).
Ex
// program Related to Garbage collection.
class FatGuy
{
FatGuy() //It is constructor
{
long l[]=new long[1000];
}
public static void main(String args[])
{
Runtime r=Runtime.getRuntime(); //creating runtime object.
System.out.println(r.freeMemory());
FatGuy f1=new FatGuy();
FatGuy f2=new FatGuy();
FatGuy f3=new FatGuy();
FatGuy f4=new FatGuy();
FatGuy f5=new FatGuy();
System.out.println(r.freeMemory());
f1=null; //removing link with objects in memory.
f2=null;
f3=null;
f4=null;
f5=null;
r.gc(); //performing GC explicity and re-claiming unused memory  (From topic Garbage Collection)
System.out.println(r.freeMemory());
}
protected void finalize() //redefining method of object class
{
System.out.println("you are in finalize() method");
}
}
Output
55710688
55710688
56320944
you are in finalize() method
you are in finalize() method
you are in finalize() method
you are in finalize() method
you are in finalize() method
FatGuy-Output-Javaform
                         To get the code file use me.

Continue to the next topic Deriving a class and polymorphism in java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required