Garbage
Collection
In the Java
programming language, dynamic allocation of objects is achieved using the new
operator. An object once created uses some memory and the memory remains
allocated till there are references for the use of the object. When there are
no references for an object, it is assumed to be no longer needed and the
memory occupied by the object can be reclaimed.
There is no
explicit need to destroy an object as java handles the de-allocation
automatically. The technique that accomplishes this is known as Garbage
Collection. Programs that do not de-allocate memory can eventually crash
when there is no memory left in the system to allocate. These programs are said
to have memory leaks
In Java,Garbage
collection happens automatically during the lifetime of a java program, eliminating the
need to de-allocate memory and avoiding memory leaks.
In C language,
it is the programmer’s responsibility to de-allocate memory allocated
dynamically using free() function. Note : All objects are created in Heap
Section of memory.
Garbage Collection Mechanism in Java
Step 1) Copy the
following code into a editor
class Student{
int a;
int b;
public void setData(int c,int d){
a=c;
b=d;
}
public void showData(){
System.out.println("Value of a = "+a);
System.out.println("Value of a = "+b);
}
public static void main(String args[]){
Student s1 = new Student();
Student s2 = new Student();
s1.setData(1,2);
s2.setData(3,4);
s1.showData();
s2.showData();
//Student s3;
//s3=s2;
//s3.showData();
//s2=null;
//s3.showData();
//s3=null;
//s3.showData();
}
}
Step 2) Save, Compile
and Run the code. As shown in the diagram , two objects and two reference
variables are created.
Step 3) Uncomment line
# 20,21,22. Save , compile & run the code.
Step 4) As show in diagram below, two reference variables are pointing to the same object.
Step 5) Uncomment line
# 23 & 24. Compile , Save & Run the code
Step 6) As show in
diagram below , s2 becomes null , but s3 is still pointing to the object and is
not eligible for garbage collection.
Step 7) Uncomment line
# 25 & 26 . Save , Compile & Run the Code
Step 8) At this point
there are no references pointing to the object and becomes eligible for garbage
collection. It will be removed from memory and there is no way of retrieving it
back
class string
{
public static void main(String args[])
{
String s1= new String("RAMU");
String s2= new String(s1);
String s3="DAYINABOYINA";
char c[]={'a','n','j','a'};
String s4=new String(c);
System.out.println("THE CONTENT OF S4 IS "+s4);
char[] d={'J','A','V','A'};
String s5=new String(d);
System.out.println("THE CONTENT OF S5 IS "+s5);
System.out.println("THE LENGTH OF THE 3 strings are"+"\n"+s1.length()+"\n"+s2.length()+"\n"+s3.length());
System.out.println("char at "+s1.charAt(1));
if(s1.equals(s2))
System.out.println("S1==S2");
if(s2.equals(s3))
System.out.println("S2==S3");
if(s3.equals(s1))
System.out.println("S3==S1");
}}
output:-
e:\jit>javac string.java
e:\jit>java string
THE CONTENT OF S4 IS anja
THE CONTENT OF S5 IS JAVA
THE LENGTH OF THE 3 strings are
4
4
12
char at A
S1==S2
//recursion
class rec
{
int f;
int fact(int n)
{
if(n==1)
return 1;
else
f=n*fact(n-1);
return f;
}}
class recimpl
{
public static void main(String args[])
{
rec r= new rec();
int n=r.fact(5);
System.out.println("THE FACTORIAL IS "+n);
}}
output:-
e:\jit>javac recimpl.java
e:\jit>java recimpl
THE FACTORIAL IS 120
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి