Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
What is finalize() method regarding Garbage Collection
Previous Home Next

In java if an object has no object references pointing to it, can be deleted by the garbage collector to regain the memory. On the other hand, if an object has a finalize() method then it will be executed before regaining the memory in order to give the object a last chance to clean up itself i.e. to release all the resources that the object was using.

The finalize() method is inherited from the Object class by any class you define. The syntax of the finalize() method declaration is shown below,


protected void finalize()

Inside this finalize() method you must specify the actions that are needed to be performed before an object is recycled. The finalize() is only called just prior to garbage collection to if any resource was left to be released .It is advised that not use finalize() method just to be on a safer side. The program must release the resources in program itself, since garbage collectors run is not guaranteed, so finalize() execution will also not be guaranteed.

The Java programming language specifies that the finalize() method will be called before the object memory is reclaimed, but it does not guarantee exactly when it will happen. Note that the finalize() method which a class inherited does not do anything,thus, if you want your object to clean up itself, you have to override the finalize() method.

Also remember that, the finalize() method is never invoked more than once by a Java virtual machine for any given object. Generally, it is considered a good programming practice to invoke the finalize() of the superclass, i.e.,


protected void finalize()
{
super.finalize();
// some other logic
}

The following code snippet will guide you how to use finalize() method during the garbage collection,

Example


Java Tutorialslass UsingFinalize 
	{
     public static void main(String[] args) 
	 {
Runtime rt = Runtime.getRuntime();
System.out.println("Total available memory:"+rt.totalMemory());
System.out.println("Available Free Memory: " + rt.freeMemory());
for(int j=0; j<1000; j++ ) 
		   {
  FinalizeMethod obj = new FinalizeMethod(j); 
 }
     System.out.println("Free Memory before call to gc(): " + 
     rt.freeMemory());
     System.runFinalization();
     System.gc();
     System.out.println(" Free Memory after call to gc(): " + 
     rt.freeMemory()); 
    }
}
Java Tutorialslass FinalizeMethod 
	{
     String str;
     int id;
     FinalizeMethod(int id) 
	  {
       this.str = new String("r4r tefsoft solutions"); 
       this.id = id;
      }
      protected void finalize()
	   {
   System.out.println("FinalizeMethod object " + id + " has been finalized.");
       }
      }

Previous Home Next