Garbage Collection in Java

By: Ivan Lim Printer Friendly Format    

Since objects are dynamically allocated by using the new operator, you might be wondering 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. There is no explicit need to destroy objects as in C++. Garbage collection only occurs sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are no longer used. Furthermore, different Java run-time implementations will take varying approaches to garbage collection, but for the most part, you should not have to think about it while writing your programs.

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 window 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.
To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that method whenever it is about to recycle an object of that class. 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
}

Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class. It is important to understand that finalize( ) is only called just prior to garbage collection. It is not called when an object goes out-of-scope, for example. This means that you 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.

Note: If you are familiar with C++, then you know that C++ allows you to define a destructor for a class, which is called when an object goes out-of-scope. Java does not support this idea or provide for destructors. The finalize( ) method only approximates the function of a destructor. As you get more experienced with Java, you will see that the need for destructor functions is minimal because of Java's garbage collection subsystem.



Ask a Question



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Ask a Question

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Sir , I am doing BCA . I want to know more about j
View Tutorial          By: sandeep Namdev at 2008-09-16 05:47:22

2. You have to the point codes
View Tutorial          By: workneh yilma at 2009-01-21 02:29:21

3. I have a computer science lab final on tuesday (09
View Tutorial          By: kudas at 2009-06-07 10:55:06

4. Sir , I am doing MSc(swe) . I want to know more ab
View Tutorial          By: guru prakash at 2009-10-15 09:10:05

5. I want to know more about Java Garbage collection,
View Tutorial          By: Prosenjit Kundu at 2010-05-08 09:42:40

6. I want to know the difference between java and .ne
View Tutorial          By: Jack at 2010-06-10 07:40:35

7. Sir , I am doing BCS . I want to know more about
View Tutorial          By: Sachin deshmukh at 2010-08-04 02:24:59

8. Sir i am doing b.tech and i want to know about Gar
View Tutorial          By: waseem at 2010-10-10 01:28:42

9. I want to know java in details.Please send me.I sh
View Tutorial          By: Rajib Halder at 2010-10-23 14:39:39

10. Sir , I am doing B.Tech . I want to know more abou
View Tutorial          By: Rakesh yadav at 2011-01-18 22:23:22

11. Sir , I am doing BCA . I want to know more about j
View Tutorial          By: sikandar at 2011-05-22 03:09:40

12. Thank you sir for your valuable guidence on garbag
View Tutorial          By: Rahul at 2011-06-13 04:40:52

13. i am a bca student. i want to know more about garb
View Tutorial          By: ahana at 2011-06-26 11:02:36

14. sir,i m doing ME/CSE.i want to know more informati
View Tutorial          By: m.rajalakshmi at 2011-07-27 05:18:19

15. sir,i m doing ME/CSE.i want to know more informati
View Tutorial          By: m.rajalakshmi at 2011-07-27 05:20:44

16. Sir , I am doing B.Tech . I want to know more abou
View Tutorial          By: amita saini at 2011-08-05 13:07:35

17. This was really helpful. It helped me have a bette
View Tutorial          By: Pradeepa at 2011-09-09 18:25:51

18. respected Sir , I am doing B.Tech . I want to know
View Tutorial          By: naveen juneja at 2011-09-18 07:54:57

19. respected Sir , I am doing B.Tech . I want to know
View Tutorial          By: naveen juneja at 2011-09-18 07:58:04

20. sir
m a bca student. I want to know more ab

View Tutorial          By: aileen Mohanty at 2011-09-23 02:06:01

21. Those who want the details of JAVA should search f
View Tutorial          By: Amit Singh at 2011-11-13 11:42:42

22. sir, could you please tell me about minimum and ma
View Tutorial          By: priya dwivedi at 2012-01-04 15:01:03

23. You have shared here much better thing which is he
View Tutorial          By: php development at 2012-01-25 05:55:46

24. Dear Sir,

I have done my B.E Degree

View Tutorial          By: amit at 2012-02-03 11:07:35

25. Dear sir i am complete my B.Tech in this year but
View Tutorial          By: Sarvesh at 2012-04-17 15:16:46

26. sir i am mca student. i can choose my flatform is
View Tutorial          By: sridevi at 2012-06-06 10:21:39

27. sir plz give ur sugissions for my golden future in
View Tutorial          By: sridevi at 2012-06-06 10:26:36

28. sir , i'm doing BCA . I want to learn more about
View Tutorial          By: gayatri patil at 2012-08-18 10:08:32

29. thank u sir ,this helped a lot
************

View Tutorial          By: yugandharr at 2012-09-11 15:47:19

30. All contains here are copied from " Complete
View Tutorial          By: Vikas at 2012-12-31 09:09:47

31. Thanks sir
View Tutorial          By: sivaraj at 2013-01-23 05:43:35