Garbage Collection in Java

By: Ivan Lim Emailed: 1602 times Printed: 2075 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(24)


1. View Comment

Sir , I am doing BCA . I want to know more about java language so please send me detail about java language.
Thanks;


View Tutorial          By: sandeep Namdev at 2008-09-16 05:47:22
2. View Comment

You have to the point codes

View Tutorial          By: workneh yilma at 2009-01-21 02:29:21
3. View Comment

I have a computer science lab final on tuesday (09 June 2009) and I an example about the garbage collection and the finalize method so I can understand them better, because I don't really get it.
So please if anyone can help me I'll be so greatfull!!!!
Thanks, bye!
kudas xoxo.


View Tutorial          By: kudas at 2009-06-07 10:55:06
4. View Comment

Sir , I am doing MSc(swe) . I want to know more about java language so please send me detail about java language.

View Tutorial          By: guru prakash at 2009-10-15 09:10:05
5. View Comment

I want to know more about Java Garbage collection,Thread,JUnit etc.
Please provide me help.

With Thanks

Prosenjit Kundu


View Tutorial          By: Prosenjit Kundu at 2010-05-08 09:42:40
6. View Comment

I want to know the difference between java and .net on garbage collection. Please send me the difference. Thanks

View Tutorial          By: Jack at 2010-06-10 07:40:35
7. View Comment

Sir , I am doing BCS . I want to know more about Java Garbage collectionso please send me detail about java language.
Thanks.


View Tutorial          By: Sachin deshmukh at 2010-08-04 02:24:59
8. View Comment

Sir i am doing b.tech and i want to know about Garbage collection and RMI (Remote Method Invocation). because day after tomarroe is exam...so plz send me that ...

View Tutorial          By: waseem at 2010-10-10 01:28:42
9. View Comment

Sir , I am doing B.Tech . I want to know more about java language so please send me detail about java language.
Thanks;


View Tutorial          By: Rakesh yadav at 2011-01-18 22:23:22
10. View Comment

Sir , I am doing BCA . I want to know more about java language so please send me detail about java language.
Thanks;


View Tutorial          By: sikandar at 2011-05-22 03:09:40
11. View Comment

Thank you sir for your valuable guidence on garbage collection

View Tutorial          By: Rahul at 2011-06-13 04:40:52
12. View Comment

i am a bca student. i want to know more about garbage collection.can u plz.............i shall be ever greatful to u

View Tutorial          By: ahana at 2011-06-26 11:02:36
13. View Comment

sir,i m doing ME/CSE.i want to know more information of garbage collection sir .so pls send more details sir pls sir

View Tutorial          By: m.rajalakshmi at 2011-07-27 05:20:44
14. View Comment

Sir , I am doing B.Tech . I want to know more about java language so please send me detail about java language.
Thanks;


View Tutorial          By: amita saini at 2011-08-05 13:07:35
15. View Comment

respected Sir , I am doing B.Tech . I want to know more about in java language reflection, therding,garbadge collection so please send me detail about in this chepter in java language with why use these concept.
Thanks;


View Tutorial          By: naveen juneja at 2011-09-18 07:58:04
16. View Comment

Those who want the details of JAVA should search for it on Internet or can buy a Complete Reference Book....

View Tutorial          By: Amit Singh at 2011-11-13 11:42:42
17. View Comment

sir, could you please tell me about minimum and maximum garbage collection

View Tutorial          By: priya dwivedi at 2012-01-04 15:01:03
18. View Comment

Dear Sir,

I have done my B.E Degree in IT (2010).and i am job less.
i think that i am weak in java.please guide me.what should i do now?


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

Dear sir i am complete my B.Tech in this year but i am eeak in java how may i increase my programming in java give me suggestion please

View Tutorial          By: Sarvesh at 2012-04-17 15:16:46
20. View Comment

sir i am mca student. i can choose my flatform is java.But,now i am in lot of confusion for which one is better course for me. plz guide me sir.

View Tutorial          By: sridevi at 2012-06-06 10:21:39
21. View Comment

sir plz give ur sugissions for my golden future in software feild.
I have lot of interest in java but i dont know how to implement.plz guide me sir.


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

sir , i'm doing BCA . I want to learn more about the java language plz send details about java language......

with thanks
GAYATRI


View Tutorial          By: gayatri patil at 2012-08-18 10:08:32
23. View Comment

thank u sir ,this helped a lot
*************************
plz give explanation on the following

->how the object will be un referenced and how it will be done
->how gc will catch un referenced object .
******************************
thanks in advance sir,


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

Thanks sir

View Tutorial          By: sivaraj at 2013-01-23 05:43:35

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ivan Lim
Requesting a Signed Certificate from a Certification Authority example using keytool in Java
The if-then Statement in Java
SELECT Statements
The BodyTag Interface in JSP
Handling Duplicate Form Submissions in Struts
Standard Input and Output in C
A sample that shows Java Beans, Servlets and JSP working together
Recursion in C
Using Multibox in Struts
switch in C
Word Counting sample program in C
Java Bean Scopes in JSF
Using cout.width() in C++
Types of configurations in J2ME
How to get the CLDC and MIDP version from a J2ME program

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
Integer: byte, short, int, and long data types in Java
Student marks calculation program in Java
XML and Java - Parsing XML using Java Tutorial
Stack example in Java - push(), pop(), empty(), search()
Count number of vowels, consonants and digits in a String in Java
FileReader and FileWriter example program in Java
How to Send SMS using Java Program (full code sample included)
TCP Server and TCP Client in Java
append() in Java
Read from a COM port using Java program
indexOf( ) and lastIndexOf( ) in Java
left shift operator, <<, in Java
Calculate gross salary in Java
Multidimensional or arrays of arrays in Java
Multiple Inheritance sample in Java
Most Emailed Articles (in Java)
What is Java?
How to Send SMS using Java Program (full code sample included)
inheritance in Java
Handling Fractions in Java
instanceof sample program in Java
Use of return statement in Java
Introducing Nested and Inner Classes
CharSequence Interface in Java
Hashtable example in Java
Using PrintWriter in Java
Method Overriding in Java
Using StringTokenizer in Java
Recursion in java
The java Buzzwords
Student marks calculation program in Java