Garbage Collection in Java

By: Ivan Lim  

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.




Archived Comments

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

2. All contains here are copied from " Complete Refrence of TATA mcgrow hill"...
View Tutorial          By: Vikas at 2012-12-31 09:09:47

3. thank u sir ,this helped a lot
*************************
plz give explanation on the f

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

4. sir , i'm doing BCA . I want to learn more about the java language plz send details about java lan
View Tutorial          By: gayatri patil at 2012-08-18 10:08:32

5. sir plz give ur sugissions for my golden future in software feild.
I have lot of interest in

View Tutorial          By: sridevi at 2012-06-06 10:26:36

6. sir i am mca student. i can choose my flatform is java.But,now i am in lot of confusion for which on
View Tutorial          By: sridevi at 2012-06-06 10:21:39

7. Dear sir i am complete my B.Tech in this year but i am eeak in java how may i increase my programmin
View Tutorial          By: Sarvesh at 2012-04-17 15:16:46

8. Dear Sir,

I have done my B.E Degree in IT (2010).and i am job less.
i think th

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

9. You have shared here much better thing which is helped me so much and I think it will help to many o
View Tutorial          By: php development at 2012-01-25 05:55:46

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

11. Those who want the details of JAVA should search for it on Internet or can buy a Complete Reference
View Tutorial          By: Amit Singh at 2011-11-13 11:42:42

12. sir
m a bca student. I want to know more about java language so please send me detail about j

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

13. respected Sir , I am doing B.Tech . I want to know more about in java language reflection, therding,
View Tutorial          By: naveen juneja at 2011-09-18 07:58:04

14. respected Sir , I am doing B.Tech . I want to know more about in java language reflection, therding,
View Tutorial          By: naveen juneja at 2011-09-18 07:54:57

15. This was really helpful. It helped me have a better understanding of garbage collection.
View Tutorial          By: Pradeepa at 2011-09-09 18:25:51

16. Sir , I am doing B.Tech . I want to know more about java language so please send me detail about jav
View Tutorial          By: amita saini at 2011-08-05 13:07:35

17. sir,i m doing ME/CSE.i want to know more information of garbage collection sir .so pls send more de
View Tutorial          By: m.rajalakshmi at 2011-07-27 05:20:44

18. sir,i m doing ME/CSE.i want to know more information of java langueges sir .so pls send more details
View Tutorial          By: m.rajalakshmi at 2011-07-27 05:18:19

19. i am a bca student. i want to know more about garbage collection.can u plz.............i shall be ev
View Tutorial          By: ahana at 2011-06-26 11:02:36

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

21. Sir , I am doing BCA . I want to know more about java language so please send me detail about java l
View Tutorial          By: sikandar at 2011-05-22 03:09:40

22. Sir , I am doing B.Tech . I want to know more about java language so please send me detail about jav
View Tutorial          By: Rakesh yadav at 2011-01-18 22:23:22

23. I want to know java in details.Please send me.I shall be glad if kindly send me.

View Tutorial          By: Rajib Halder at 2010-10-23 14:39:39

24. Sir i am doing b.tech and i want to know about Garbage collection and RMI (Remote Method Invocation)
View Tutorial          By: waseem at 2010-10-10 01:28:42

25. Sir , I am doing BCS . I want to know more about Java Garbage collectionso please send me detail a
View Tutorial          By: Sachin deshmukh at 2010-08-04 02:24:59

26. I want to know the difference between java and .net on garbage collection. Please send me the differ
View Tutorial          By: Jack at 2010-06-10 07:40:35

27. I want to know more about Java Garbage collection,Thread,JUnit etc.
Please provide me help.
View Tutorial          By: Prosenjit Kundu at 2010-05-08 09:42:40

28. Sir , I am doing MSc(swe) . I want to know more about java language so please send me detail about j
View Tutorial          By: guru prakash at 2009-10-15 09:10:05

29. I have a computer science lab final on tuesday (09 June 2009) and I an example about the garbage col
View Tutorial          By: kudas at 2009-06-07 10:55:06

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

31. Sir , I am doing BCA . I want to know more about java language so please send me detail about java l
View Tutorial          By: sandeep Namdev at 2008-09-16 05:47:22


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial