Lifetime Concept in Java Programming
- Time between allocation and de-allocation of memory
- Life time depends on block i.e { }
- life time starts when declaration statement elaborated and expires at end of block
- There is no delete keyword in java
- Garbage collector is responsible for all deletes
- Java is free from dangling reference and garbage problem
Why java does not support delete keyword?
- Java does not support delete keyword because there is a garbage collector in java
- This automatically runs and collects dangling or un-referenced objects.That is why there is no delete keyword in java like C++.
- Garbage collector can be explicitly like that System.gc();
- When a life of object ends garbage collector runs automatically
Example 1
{
int a=5;// life Time start here
a++;
} //life time expire
Let Student is a class
Student s; // creating a reference of Student class
{Student t; // l
t= new Student(); // Life Time Start
s=t;
}//garbage collector expires Student t reference from stack
When life of an object ends it is available for garbage collector to collect.
{Student t;
t= new Student(); // Life Time Start
t= new Student();
t= new Student();
t= new Student();
}//life time expires
Example 4
{Student t;
t= new Student(); // Life Time Start
System.gc(); // invokeing garbage collector
t= new Student();
t= new Student();
System.gc();
t= new Student();
}//
Destructors in java
Example
class Car{
String make;
Car (String m){make=m;}
String getMake() { return make;}
void setMake(String m){make=m;}
protected void finalize(){
// action on expiry
}
}
int a=5;// life Time start here
a++;
} //life time expire
Example 2
Let Student is a class
Student s; // creating a reference of Student class
{Student t; // l
t= new Student(); // Life Time Start
s=t;
}//garbage collector expires Student t reference from stack
When life of an object ends it is available for garbage collector to collect.
Example 3
{Student t;
t= new Student(); // Life Time Start
t= new Student();
t= new Student();
t= new Student();
}//life time expires
Note: We can explicitly request garbage collector to invoke using System class gc method but it will again depends of garbage threadSystem.gc();
Example 4
{Student t;
t= new Student(); // Life Time Start
System.gc(); // invokeing garbage collector
t= new Student();
t= new Student();
System.gc();
t= new Student();
}//
Destructors in java
- No explicit destructors in java as c++
- finalize method can be used as action on expiry
Example
class Car{
String make;
Car (String m){make=m;}
String getMake() { return make;}
void setMake(String m){make=m;}
protected void finalize(){
// action on expiry
}
}
There are no explicit pointers in java like C++Problem with Pointers
1- Garbage (Memory Leakage)
c++
Student *t;
t= new Student(); // Life Time Start
t= new Student();
t= new Student();
t= new Student();
2- Dangling Reference
c++
Student *c1=new Student();
Student *c2=c1;
delete c2;
c1->getName();
Just two things:
ReplyDeleteSystem.gc() just asks the Garbage Collector to run but it isn't forced to do so (at least call it a few times will force it for sure).
The other thing is about finalize(). Yeah the method exists but there are very few situations where you want to use it. For example if you use native resources and you want to be sure they don't leak (even if people missed to explicitly close it) than you could use finalize() as a last way to force resource release.
Thanks. Christoph Englbert for Sharing Info.
Delete