aspose file tools*
The moose likes Beginning Java and the fly likes question about garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about garbage collection" Watch "question about garbage collection" New topic
Author

question about garbage collection

Hengki Widjaja
Ranch Hand

Joined: Oct 31, 2011
Posts: 44
I have a class like so:


and then another class


The question here, when testIt() completes, will the resultset in the heap be eligible for gc? notice that I never do something like rs=null. thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Hengki Widjaja wrote:The question here, when testIt() completes, will the resultset in the heap be eligible for gc? notice that I never do something like rs=null. thanks

Yes it should be, because 'rs' is local to doWork() and therefore goes out of scope as soon as the method completes. However, it looks like a very tortuous way of doing things.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Objects will be eligible for garbage collection from the moment that there is no live thread that references the object anymore.

If the local variable rs in the testIt method is the only variable that refers to the ResultSet object returned from doWork, then yes, the ResultSet object will be eligible for garbage collection from that point on.

You don't need to explicitly set variables to null.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

But you should still explicitly close the ResultSet, to free up resources immediately. This should preferably happen in a try-finally block:
This doesn't only hold for ResultSet but also Statement, PreparedStatement, Connection, and all streams, readers and writers from java.io - close them when you no longer need them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hengki Widjaja
Ranch Hand

Joined: Oct 31, 2011
Posts: 44
Yes, well, that's the whole point of my question. I just want to confirm that the object will still gc'ed even if I dont assign the references to null, right? will there be leak if I don't close or set null? thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

Eventually the ResultSet will be garbage collected, and the resources will be freed. However, you have no idea when that might happen; it could be minutes later, it could even be much later, or not occur before your application exists. That's because you only know the object can be garbage collected, but not when. Freeing such resources explicitly is a good habit, since you have control over when it occurs.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Hengki Widjaja wrote:Yes, well, that's the whole point of my question. I just want to confirm that the object will still gc'ed even if I dont assign the references to null, right? will there be leak if I don't close or set null? thanks

It depends on what you mean by 'leak'. What we have told you is that the ResultSet will be eligible for garbage collection because it will go out of scope; what Rob, quite rightly, pointed out is that there is more "under the hood" in a ResultSet which makes it advisable to also close() it when you're done with it.

There is no "magic bullet" for this stuff; just experience (usually bitter).

BTW - You do also realize that setting 'rs' to null is no guarantee that it will be garbage collected?

Winston
Hengki Widjaja
Ranch Hand

Joined: Oct 31, 2011
Posts: 44
Precisely, the JVM has full authority to decide when to start gc'ing objects. that's why I'm investigating if I can omit the nulling and closing part. who knows, I might be able to "increase" the health of my fingers and keyboard and save some line of code.
just curious. thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19791
    
  20

For stuff like Lists, Maps, etc - not a problem. Just let all references go out of scope and be done with it. Only when the object has external resources (essentially anything that implements AutoCloseable in Java 7), it should be closed explicitly.

But speaking of Java 7, if you use its try-with-resources block you can let it go out of scope (after the try) and it will be automatically closed:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hengki Widjaja wrote:Precisely, the JVM has full authority to decide when to start gc'ing objects. that's why I'm investigating if I can omit the nulling and closing part. who knows, I might be able to "increase" the health of my fingers and keyboard and save some line of code.
just curious. thanks.


You never (well, almost never) get any GC benefit by explicitly setting a reference to null. Once it's out of scope, the memory will be reclaimed no later than when it is absolutely needed in order to meet some memory allocation request, and possibly well before that time. Note, however, that GC is about reclaiming memory, not about other resources. If a socket or DB connection or whatever happens to get closed and reclaimed as a result of GC, that's a lucky side-effect, but we must not rely on GC to clean up these resources for us, only memory. And these other resources tend to be more scarce. Depending on what you're code is doing, if you don't explicitly close, you can run out of ports, file handles, etc. very quickly.

So, yes, always close (in a finally block), but don't bother with setting references to null.

(Except when taken care of by try-with in Java 7, as Rob points out.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about garbage collection