This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection / memory leakage

 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic