The Artima Developer Community
Sponsored Link

Java Answers Forum
Need clarification on GC process

2 replies on 1 page. Most recent reply: Mar 19, 2005 8:50 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Venkatesan

Posts: 9
Nickname: venkki
Registered: Jun, 2003

Need clarification on GC process Posted: Mar 16, 2005 11:50 PM
Reply to this message Reply
Advertisement
Hi..

I need clarification on how the JVM handles GC process in optimum way. Pl look into the following 2 sample and simple java classes. My doubt is whether we need to explicitly make the objects we are creating inside a method scope to null or not to make them ready for forthcoming GC. Suppose, if you don't set those objects as null, what will happen to those when GC occurs?

Pl clarify which approach will yield good GC management?

public class Sample
{
public int calBalance(String acctid){
int i = 0;
try {
AccountReposit acctobj = new AccountReposit();
i = acctobj.getBal(acctid);
}
catch(Exception e){
}
return i;
}
}

public class Sample
{
public int calBalance(String acctid){
int i = 0;
AccountReposit acctobj = null;
try {
acctobj = new AccountReposit();
i = acctobj.getBal(acctid);
}
catch(Exception e){
}
finally {
if(acctobj!=null) acctobj=null;
}
return i;
}
}

Expecting your reply.

Venkatesan.


vaidhyanathan.p.k

Posts: 3
Nickname: vaidya
Registered: Mar, 2005

Re: Need clarification on GC process Posted: Mar 17, 2005 1:50 AM
Reply to this message Reply
hi,
see u need not explicitly make the objects to null se there are two thing reachable and nonreachable.
reachable--->if the objects exist somewhere in the program(i.e ) they r used somewhere in the program then they would not be garbage collected

not reachable--->if u cant find the objects no where in the program i.e they are not used those objects will be garbage collected

ok u need not explicitly set to null if the object is not used its memory will automatically be released during garbage collection
u can enforce garbage collection in the above way

public class Sample
{
public void finalize()
{
System.out.println("garbage collection");
}
public int calBalance(String acctid){
int i = 0;
AccountReposit acctobj = null;
try {
acctobj = new AccountReposit();
i = acctobj.getBal(acctid);
System.gc();
System.out.println("check the object after garbage collection"+acctobj );
}
catch(Exception e){
}
/*finally {
if(acctobj!=null) acctobj=null;
}*/
return i;
}
}

--> see here finalize method of object has been overridden
when u call System.gc() ,the garbage collection process takes place and the finalize method wiill be called.

see there are referencce classes called
SoftReference,WeakReference,PhantomRefernce that might help
fo garbage collection,these classes are used to hold object refernces u can get mopre details about it in java.lang.ref
in javadocs

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Need clarification on GC process Posted: Mar 19, 2005 8:50 PM
Reply to this message Reply
To summarize, there's no need to set objects to null; as soon as they go out of scope (and there are no other references) they are collectable. Setting references to null only means that the objects they referred to might possibly be colected before the method returned (or before the end of the scope).

Also, if you use the java tags described in the "Formatting Your Post" box you see while composing your post, the code you post will have nice indentation and colored syntax. For example:
public class Sample
{
   public void finalize()
   {
      System.out.println("garbage collection");
   }
   public int calBalance(String acctid)
   {
      int i = 0;
 
      // ... and so on.

Flat View: This topic has 2 replies on 1 page
Topic: compiling error Previous Topic   Next Topic Topic: please help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use