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

garbage collection / memory leakage

jite eghagha
Ranch Hand

Joined: Oct 06, 2006
Posts: 127
Good day,

Using these chunks of Pseudocode below would someone please enlighten me on the art of Garbage Collection ?

What should i worry about, what commands do i use to carry out Garbage collection ? Do i handle each Object i wish to set for collection seperately, or collectively?

When i debug in Netbeans IDE, there is a bar that displays some measure of memory ? 83.9 / 89.1 mb. These numbers, do they have something to do with memory leakage ?


private Connection connection;

public DataBase()
{
DataBaseConnector DbConn = new DataBaseConnector();
connection = DbConn.getConnection();
}
....
public void closeConnection() {
connection.close();
}
....
public String getXmlResult(eRequest eRequestType) {
closeConnection();
return XML;
}
....
public String projectRecordCaseXML(eRequest eRequestType) {
PreparedStatement pSqlStmtTitle = null;
ResultSet rsTitle = null;
if(something) {
pSqlStmtTitle = connection.prepareStatement(something);
pSqlStmtTitle.setString(1,pInputStr);
rsTitle = pSqlStmtTitle.executeQuery();
}
return //call some other method//;
}


thanks
Eric Gero
Ranch Hand

Joined: Jul 20, 2005
Posts: 73
From the posting, I am not sure if this is a question on Garbage Collection in general, or if you are after something more specific. But, Java typically handles garbage collection on its own. Once the JVM determines that there are no references to the object, it becomes available to for collection. But, there is no guarantee that it will be collected, nor is there any guarantee as to when the garbage collector will run. You can invoke the garbage collector with:

But, that doesn't mean anything is going to happen. JVM's use different algorithms to determine garbage collection, and you do not have complete control over the process. It's a good habit to set objects to null, when you know you are done with them, but there is not much to worry about as far as memory leakage though, unless you are running native methods (written in C or some other language.)

Here are a couple sites that offer some good info about the process:

http://www.devdaily.com/java/edu/pj/pj010008/pj010008.shtml
http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html?page=1
[ November 01, 2006: Message edited by: Eric Gero ]
Timothy Frey
Ranch Hand

Joined: Jul 22, 2006
Posts: 56
Those numbers in that widget represent the amount of memory that the heap is currently using and the heap's current maximum size given to NetBeans itself. If you click on it you can force garbage collection. I don't think it's accurate enough for you to track down memory leaks. I know the NetBeans profiler has a bunch of useful stuff for this, however.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In general it's good to ignore GC in your code. Take care to not hold object references any longer than you really need them and let it do its thing.

Outside the code, you can do a lot of tuning on the JVM parameters. Read up on those and be careful to read for your specific JVM vendor and version for the really tricky bits.

If you have Java 5 or later, fire up JConsole. You can watch the various areas of memory grow until the JVM needs more room, then see GC clear up some space. It's not a bad thing if memory usage goes right to the limit before GC. It may well be a bad thing if the low end of the usage curve after GC creeps up over time or doesn't come back down far enough to allocate some ginormous object.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
jQuery in Action, 2nd edition
 
subject: garbage collection / memory leakage