0% found this document useful (0 votes)
84 views

Object Class, Garbage Collection and Finalize Method

The Object class sits at the top of the class hierarchy in Java. All classes inherit methods from Object like clone(), equals(), finalize(), getClass(), hashCode(), toString(), notify(), notifyAll(), wait(). The finalize() method is called by the garbage collector before an object is destroyed and allows the object to perform cleanup. It is not guaranteed when or if finalize() will be called so critical cleanup should not rely on it.

Uploaded by

Golmei Shaheam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Object Class, Garbage Collection and Finalize Method

The Object class sits at the top of the class hierarchy in Java. All classes inherit methods from Object like clone(), equals(), finalize(), getClass(), hashCode(), toString(), notify(), notifyAll(), wait(). The finalize() method is called by the garbage collector before an object is destroyed and allows the object to perform cleanup. It is not guaranteed when or if finalize() will be called so critical cleanup should not rely on it.

Uploaded by

Golmei Shaheam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Class, Garbage Collection

and Finalize() Method


Object as a Superclass

• The Object class, in the java.lang package, sits at


the top of the class hierarchy tree.
• Every class is a descendant, direct or indirect, of
the Object class.
• Every class you use or write inherits the instance
methods of Object.
• You need not use any of these methods, but, if
you choose to do so, you may need to override
them with code that is specific to your class.
• The methods inherited from Object that are
discussed in this section are:
• protected Object clone() throws CloneNotSupportedException
      Creates and returns a copy of this object.

• public boolean equals(Object obj)


      Indicates whether some other object is "equal to"
this one.

• protected void finalize() throws Throwable


      Called by the garbage collector on an object when
garbage collection determines that there are no more
references to the object
• public final Class getClass()
      Returns the runtime class of an object.
• public int hashCode()
      Returns a hash code value for the object.
• public String toString()
      Returns a string representation of the
object.
• The notify, notifyAll, and wait methods
of Object all play a part in synchronizing the
activities of independently running threads in a
program, which is discussed in a later lesson and
won't be covered here. There are five of these
methods:
• public final void notify()
• public final void notifyAll()
• public final void wait()
• public final void wait(long timeout)
• public final void wait(long timeout, int nanos)
Garbage Collection
• Since objects are dynamically allocated by using the new operator.

Q -> How such objects are destroyed and their memory released for
later reallocation.
In some languages, such as C++, dynamically allocated objects
must be manually released by use of a delete operator.

• Java takes a different approach;


• It handles deallocation for you automatically.

• The technique that accomplishes this is called garbage collection.

• It works like this: when no references to an object exist, that object


is assumed to be no longer needed, and the memory occupied by
the object can be reclaimed.
Garbage Collection
Java objects are eligible for garbage collection
(GC),
• which frees their memory
• and possibly associated resources,

when they are no longer reachable


If object has a finalize method
• • object is added to a finalization queue
• • at some point it’s finalize method is invoked

so the object can free associated resources


Finalization – opposite of initialization
• Garbage collection can ONLY free the memory
resources
• Need finalize() to free other resources, for
example, network connection, DB connection,
file handler, etc.
• finalize() takes no argument, return void
• Rarely used for application-level programming
The finalize() Method
• Sometimes an object will need to perform some
action when it is destroyed. For example, if
• an object is holding some non-Java resource such
as a file handle or character font, then you might
want to make sure these resources are freed
before an object is destroyed.
• To handle such situations, Java provides a
mechanism called finalization.
• By using finalization, you can define specific
actions that will occur when an object is just
about to be reclaimed by the garbage collector.
• Inside the finalize( ) method, you will specify
those actions that must be performed before an
object is destroyed.
• The garbage collector runs periodically, checking
for objects that are no longer referenced by any
running state or indirectly through other
referenced objects.
• Right before an asset is freed, the Java run time
calls the finalize( ) method on the object.
The finalize( ) method has this
general form:
protected void finalize( )
{
// finalization code here
}
The finalize() Method
• The Object class provides a callback
method, finalize(), that may be invoked on an
object when it becomes garbage. 
• Object's implementation of finalize() does
nothing—you can override finalize() to do
cleanup, such as freeing resources.
• The finalize() method may be called
automatically by the system, but when it is
called, or even if it is called, is uncertain.
• Therefore, you should not rely on this method
to do your cleanup for you.
• For example, if you don't close file descriptors
in your code after performing I/O and you
expect finalize() to close them for you, you
may run out of file descriptors.
• It is important to understand that finalize( ) is
only called just prior to garbage collection.
• U cannot know when—or even if—finalize( )
will be executed.
• Therefore, your program should provide
other means of releasing system resources,
etc., used by the object.
• It must not rely on finalize( ) for normal
program operation.

You might also like