File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Object being garbage Collected by programmer or by JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object being garbage Collected by programmer or by JVM " Watch "Object being garbage Collected by programmer or by JVM " New topic
Author

Object being garbage Collected by programmer or by JVM

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi

Please tell me who makes Garbage Collection .

IF i have a Object for example a Connection Object in JDBC with instance name as conn.

IF i say conn==null.


with this line conn==null

is the programmer making the Garbage collection ??

Or this will help JVM to make the conn Object Garbage Collected ??

share your knowledge .

Thanks in advance .


Save India From Corruption - Anna Hazare.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Removing all references to an object (setting those variables to null) will make that object eligible, but there's no guarantee it will be collected while the program is running.

You can't force or guarantee garbage collection. (Note that calling System.gc() only "suggests that the Java Virtual Machine expend effort toward recycling unused objects.")


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sujith Acharya
Ranch Hand

Joined: Dec 25, 2006
Posts: 60
when you say conn=null, then its an indication to JVM is that, the object which conn is pointing is eligible for garbage collection(provided there no other reference to that object)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Objects are garbage collected when no live thread can access then. One way of achieving that is by nulling all the references to that object.
But that doesn't work always:



There are still references to the Test objects however no live thread can access them. The JVM recognizes these Islands of Objects and GC'd them.
Run:
wouter@wouter-laptop:~/temp$ javac Test.java
wouter@wouter-laptop:~/temp$ java -verbose:gc Test
Start
[GC 232K->120K(32256K), 0.0009260 secs]
[Full GC 120K->103K(32256K), 0.0037630 secs]
End


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
By making reference to null, you are telling JVM that i do not need this object anymore and you may garbage collect it at your own convenient time unless the no more reference is given to this object else where(this includes finalized method).


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Wouter Oet wrote:Objects are garbage collected when no live thread can access then.

Well, not necessarily--that's when they're *eligible*, but not necessarily when they're actually collected.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you all , for sharing knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object being garbage Collected by programmer or by JVM