我遇到了代码问题。在MainFrame.java中,我正在创建一个新的JDialog,它将一个ResultSet从derby数据库加载到jTable。我在数据库中插入了150000行用于测试目的。将ResultSet中的值加载到jTable(在LoadTable()方法中),将java.exe系统资源内存增加大约500 MB。 问题是在处理JDialog时,使用的500 MB未被释放。只有在MainFrame.java中调用system.exit()时才会释放它们。
当我重新打开JDialog时,内存资源增加到800mb。第三次重新打开JDialog会将内存增加到1 Gb,依此类推。
以下是使用的代码:
1)调用JDialog:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Database.GetConnection().commit();
}
catch (SQLException ex)
{
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "jMenuItem1ActionPerformed() Method in MainFrame.Java, Database error!", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
Insert ins = new Insert(this, true);
ins.setVisible(true);
}
2)将值加载到jTable:
private void LoadTable() throws SQLException
{
Timestamp tstart = new Timestamp(new java.util.Date().getTime());
//get all from database
ResultSet rs;
rs = Database.GetCutiiAll();
Timestamp tstop1 = new Timestamp(new java.util.Date().getTime());
//create a model for the table
int len = Database.GetColoaneArr().length + 1;
javax.swing.table.DefaultTableModel tableModel = new DefaultTableModel(0, len);
//set table headers for columns
String[] headers = new String[Database.GetColoaneArr().length + 1];
headers[0] = "ID";
System.arraycopy(Database.GetColoaneArr(), 0, headers, 1, headers.length - 1);
tableModel.setColumnIdentifiers(headers);
//set selection mode
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//insert rows to table
int rowNo = 0;
while (rs.next())
{
//create object array with values for table columns
int length = headers.length;
Object[] data = new Object[length];
for (int i = 0; i < length; i++)
{
//parse values where are foreign keys in the database
Object val = rs.getObject(headers[i]);
switch (headers[i])
{
case "SERIE":
{
String[] temp = Database.GetSerii();
int index = (int) val;
data[i] = temp[index - 1];
break;
}
case "PROTECTIE_EX_1":
case "PROTECTIE_EX_2":
case "PROTECTIE_EX_3":
{
String[] temp = Database.GetProtectii();
int index = (int) val;
data[i] = temp[index - 1];
break;
}
case "TIP_MATERIAL":
{
String[] temp = Database.GetMateriale();
int index = (int) val;
data[i] = temp[index -1];
break;
}
default:
{
data[i] = rs.getObject(headers[i]);
break;
}
}
}
tableModel.insertRow(rowNo, data);
rowNo++;
}
//attach model to table
table1.setModel(tableModel);
//create listeners to update database when changing values
Timestamp tstop2 = new Timestamp(new java.util.Date().getTime());
//show how long it took to load the database
long s = tstop2.getTime() - tstart.getTime();
jDurata.setText(String.valueOf(s) + " ms");
}
3)处置JDialog:
private void jBtnIesireActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (Database.GetCanSave())
{
int answer = MessageBox.ShowConfirmationMessage(this, "Salvati modificarile inainte de a iesi?", "OPTIUNE", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (answer)
{
case JOptionPane.CANCEL_OPTION: break;
case JOptionPane.YES_OPTION: {
try
{
Database.Save();
}
catch (SQLException ex)
{
Logger.getLogger(Insert.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "Database.Save() Method in Insert.Java, Failed to update database.", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
break;
}
case JOptionPane.NO_OPTION: {
try
{
Database.RollbackAllSavepoints();
}
catch (SQLException ex)
{
Logger.getLogger(Insert.class.getName()).log(Level.SEVERE, null, ex);
Log.GetLogger().log(Level.ALL, "Database.Undo() Method in Insert.Java, Failed to update database.", ex);
MessageBox.ShowMessage(this, ex.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
finally
{
this.dispose();
}
}
default: break;
}
}
else
this.dispose();
}
先谢谢,抱歉,我不能在这里格式化代码。
答案 0 :(得分:1)
将150000条记录加载到内存中是一个非常糟糕的主意。我相信你应该有一个分页,一次只加载一小块内存,就像10条记录一样。如果您将策略切换为使用分页,那么您的内存问题将得到解决。
答案 1 :(得分:1)
这是了解您的记忆使用位置的绝佳工具,以及原因:https://eclipse.org/mat/
我推测:你调用tableModel.insertRow的次数越多,你使用的内存就越多。
但是运行一个真正的记忆分析工具而不是猜测,因为通常你的第一个猜测是错误的,工具会告诉你真相。