Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java Garbage Collection Tutorial using source code examples


Share
 
 

Useful Java Links


       


Java Garbage Collection

 

·           When an object is no longer referred to by any variables , Java automatically reclaims the memory used by that object. This is known as garbage collection.

·         When we execute a software application it uses the memory in several different ways like used to create stack, a heap, in Java's case constant pools, and method areas. The heap is that part of memory where Java objects live, and it is the only  part of the memory involved in the garbage collection process. So Garbage collection ensures that heap has as much free space as possible.

·          When garbage collector runs its main purpose is to find and delete objects that cannot be reached.

·          If we think of a Java program as being  in a constant  cycle of creating a new object when it needs, and then discarding them when they are no longer needed, creating objects,discarding them and so on. When Garbage Collector runs it look for for those  discarded objects and deletes them from memory.

·         The garbage collector is under the control of the JVM. JVM decides when to run the garbage collector.

·         From within our Java program we can ask JVM to run the garbage collector , the JVM will grant the request but there is no guarantees. Normally JVM will run the garbage collector when it senses that memory is running low.

·         Object is eligible for garbage collection when no live thread can access it. Means object that can't be reached by any live thread, it will consider that object is eligible for deletion. Reaching an object means having reachable reference variables referring to the object and that reference variable is used by live thread, then that object is considered reachable.

·         There are many ways to  make an object unreachable and make eligible for garbage collection.

 

·         Nulling a Reference

                        Example to show how to make object eligible for garbage collection using nulling                           reference

 

                                                public class GarbageSample {

           

                                    public static void main(String[] args) {

                                    // TODO Auto-generated method stub

                                    StringBuffer sb = new StringBuffer("hello");

                                    System.out.println(sb);

                                    //The StringBuffer object not eligible for garbage collection

                                    sb=null;

                                    // Now StringBufferObject eligible for garbage collection.

                                    }

                                                }                     

 

·         Reassigning a Reference Variable

                        Examine the following code how object made unreachable.

                                public class GarbageSample {

                       

                        public static void main(String[] args) {

                        // TODO Auto-generated method stub

                        StringBuffer sb1 = new StringBuffer("hello");

                        StringBuffer sb2 = new StringBuffer("Java World");

                        System.out.println(sb1);

                        //At this point the StringBuffer "hello" not eligible for garbage                                    collection

                        sb1=sb2;

                        // Redirects sb1 to refer to the"Java World" object.

                        // Now the StringBuffer "hello"is  eligible for garbage collection.

                       

                        }

 

                        }

 

·         Isolating a  Reference

                        Here object become eligible for garbage collection even if they still have valid references. We call this  scenario “islands of isolation

                        A simple example to explain what is islands of isolation : Consider a class that  has a instance variable that is a reference variable to another instance of the same class. Now imagine two such instance exist and that they refer to each other. If  all reference to these two objects are removed , then even through each object still had a valid reference , there will no way for live thread to access either object. When garbage collector runs, it can usually find such island of objects and remove them.  Examine the following code

 

                                public class Island {

                        Island i;

                        public static void main(String args[]){

                                    Island i2= new Island();

                                    Island i3= new Island();

                                    Island i4= new Island();

                       

                                    i2.i= i3; // i2 refers i3

                                    i3.i = i4; //i3 refers i4

                                    i4.i= i2; //i4 refers i2

                       

                                    i2=null;

                                    i3=null;

                                    i4= null;

                                    //Here instance variables of objects refer to each other,

                                    //but we can't access objects from outside world because it have                                //been             nulled.

 

                                    //some other code

                        }

 

                        }

 

·         Forcing Garbage collection:

                        Java provide some methods that allow we to request that the JVM perform garbage collection. Garbage collection routines that Java provides are members of Runtime class. Runtime class is Singleton class. The Runtime object provides a mechanism to communicate with VM. To get Runtime object we can use the method                                                         

Runtime.getRuntime(). Once we have Runtime object we can invoke the garbage collector using the gc() method. We can call the gc()  method on the System class. The simplest way to ask for garbage collection is                                                       

System.gc();




Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on java-garbage-collection-tutorial, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on java-garbage-collection-tutorial, to receive updates on java-garbage-collection-tutorial,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: java-garbage-collection-tutorial tutorial,java-garbage-collection-tutorial in java,Concepts of java-garbage-collection-tutorial,Java,J2EE,Interview Questions,java-garbage-collection-tutorial Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US