When I run the following code in the profiler, I get a char[] and byte[] that build up until the program crashes due to a java heap out of memory exception. Can someone tell me why? Perhaps I am doing something fundamentally wrong.
package testleak;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.swing.Timer;
public class TestLeak
{
static String DB_USERNAME = "userName";
static String DB_SUBSCRIPTION_EXPIRATION = "subscriptionExpiration";
static String DB_REMOTE_ACCESS_ENABLED = "remoteAccessEnabled";
static String DB_LOCAL_USERNAME = "root";
static String DB_LOCAL_PASS = "root";
public static void main(String[] args)
{
Timer timer = new Timer(2000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent evt)
{
TestLeak tester = new TestLeak();
try
{
tester.go();
}
catch (NumberFormatException n)
{
}
tester = null;
}
});
timer.start();
while (true)
{
//keep the program from ending...
}
}
private void go() throws NumberFormatException
{
ResultSet results = null;
Connection conn = null;
Properties connectionProps = new Properties();
try
{
connectionProps.put("user", "root");
connectionProps.put("password", "root");
conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/myDataBase",
connectionProps);
connectionProps = null;
try
{
String rawQuery = new String("SELECT " + TestLeak.DB_USERNAME + ", "
+ TestLeak.DB_REMOTE_ACCESS_ENABLED
+ ", " + TestLeak.DB_SUBSCRIPTION_EXPIRATION + " FROM myTable");
Statement statement = conn.createStatement();
try
{
statement.executeQuery(rawQuery);
results = statement.getResultSet();
rawQuery = null;
try
{
while (results.next())
{
String auth = new String(results.getString(TestLeak.DB_REMOTE_ACCESS_ENABLED));
if (auth.equals("1"))
{
Long subExpires = Long.valueOf(results.getString(TestLeak.DB_SUBSCRIPTION_EXPIRATION));
if (subExpires > System.currentTimeMillis())
{
System.out.println(results.getString(TestLeak.DB_USERNAME));
System.out.println();
}
subExpires = null;
}
auth = null;
}
}
finally
{
results.close();
}
}
finally
{
statement.close();
}
}
finally
{
conn.close();
}
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
}
I think I am releasing everything, but something must be preventing all objects from being released. Why is it that all objects are not eligible for garbage collection when the go() method ends? Every time I envoke garbage collection in the profiler I get another surviving generation. Thanks.