的OutOfMemoryError

时间:2013-05-13 20:58:35

标签: java mysql

我正在制作一个时间表调度程序作为最后一年的项目。在过去的两天里,我得到了一个OutOfMemoryException。我已经阅读了很多关于异常的内容,并尝试通过-Xms和-Xmx选项增加分配的内存。这些似乎都不适合我。

我对项目进行了分析,发现hashmap对象以及MySQL连接都消耗了最大空间。我使用了如下的静态连接

public final class Connector
{
private static Connector connector;
Connection con;
String driverName;
String dbname;
String username;
String password;
String connString;

private Connector(){
    driverName = "com.mysql.jdbc.Driver";
    dbname = "timegen";
    username = "root";
    password = "root";
    connString = "jdbc:mysql://localhost:3306/" + dbname;
    openConnection();
}

public void openConnection(){
    try{
        Class.forName(driverName);

        con = DriverManager.getConnection(connString, username, password);
    } catch(Exception e){
        System.out.println(e);

    }
}


public void terminateConnection(){
    try{
        con.close();
    } catch(Exception e){
        System.out.println(e);
    }

}

public static Connector createConnection() {

    if (connector == null){
        connector = new Connector();
    }

    return connector;
}

public Connection getCon() {
    return con;
}
public String getConnString() {
    return connString;
}

public void setConnString(String connString) {
    this.connString = connString;
}

}

这是名为MasterData的类的代码,该类由访问数据库的所有其他类扩展

public class MasterData{
    static Connector con;
    static Statement st;

    MasterData(){
    try {
        con = Connector.createConnection();
        st = con.getCon().createStatement();
    } catch (SQLException ex) {
        Logger.getLogger(MasterData.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    public Statement createStatement() throws SQLException{
        Statement st = con.getCon().createStatement();
        return st;       
    }
    public void closeConnection(){
        con.terminateConnection();
    }


}

使用此

的类的示例
public class Teacher extends MasterData{

int teacherid;
String teachername;
String subject;
String post;

@Override
public String toString() {
    return "Teacher{" + "teacherid=" + teacherid + ", teachername=" + teachername + ", 
post=" + post + ", subject=" + subject + '}';
}



public Teacher(int teacherid, String teachername,String subject, String post) {
    this.teacherid = teacherid;
    this.teachername = teachername;
    this.subject = subject;
    this.post = post;
}

public Teacher(String teachername) {
    this.teachername = teachername;
}

public Teacher(){}

public String display(){

    String s ="\nTeacher name = " + teachername
            + "\nSubject = " + subject
            + "\nPost = "+post;
    return s;
}


public ArrayList<String> getSubjectTeachers(String s){
    ArrayList<String> teachers = new ArrayList<String>();
    try{
        ResultSet rs = st.executeQuery("select teachername from teacher where 
subject='"+s+"';");
        while(rs.next()){
            teachers.add(rs.getString(1));
        }
    }catch(Exception e){e.printStackTrace();}

    return teachers;
}

public List<Teacher> getFree()
{
   List<Teacher> lst = new ArrayList<Teacher>();
   try{
       ResultSet rs = st.executeQuery("select * from teacher where teacherid not 
in(select classteacher from division where classteacher!=null)");
       while(rs.next())
       {
            lst.add(new 
 Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)));
       }

   }catch(Exception e ){e.printStackTrace();}
   return lst;
}




public int getTeacherid() {
    return teacherid;
}

public void setTeacherid(int teacherid) {
    this.teacherid = teacherid;
}

public String getTeachername() {
    return teachername;
}

public void setTeachername(String teachername) {
    this.teachername = teachername;
}


public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public String getPost() {
    return post;
}

public void setPost(String post) {
    this.post = post;
}

public boolean checkDuplicate(){
    try{
        ResultSet rs = st.executeQuery("select * from teacher where 
teachername='"+teachername+"';");
        if(rs.next())
            return true;

    }catch(Exception e){e.printStackTrace();}

         return false;

}

public boolean insert(){
    int t;
    try{
        t = st.executeUpdate("insert into teacher(teachername,subject,post)     
values('"+teachername+"','"+subject+"','"+post+"');");
        if(t!=0) return true;
    }
    catch(Exception e){
        e.printStackTrace();
        return false;

    }
    return false;
}


public boolean delete(){
    int t;
    try{
        new AssignedTeacher().deleteTeacher(teacherid);
        t = st.executeUpdate("delete from teacher where teacherid="+teacherid+";");
        if(t!=0) return true;
    }
    catch(Exception e){
        e.printStackTrace();
        return false;

    }
    return false;
}

public boolean update(){
    int t;
    try{
        t = st.executeUpdate("update teacher set teachername = '"+teachername+"',                  
subject='"+subject+"', post='"+post+"' where teacherid="+teacherid+";");

        if(t!=0) return true;
    }
    catch(Exception e){
        e.printStackTrace();
        return false;

    }
    return false;
}

}

我的目的是为整个程序创建一个静态连接。它似乎运作良好。但这是问题的可能原因吗?

2 个答案:

答案 0 :(得分:0)

您似乎创建了太多Connection s。

您可以在openConnection方法中验证您的关联is valid是否也可以使用某些Connection Pool

修改

在我看来,您已尝试实施Active record pattern,因为有insertdeleteupdategetSubjectTeachers方法。无论如何,extend Teacher from MasterData并不总是一个好主意。作为副作用,将为MasterData的每个实例创建新连接。 static Connection con将被重新分配给新对象,但之前的Connection将不会被关闭。与MasterData#createStatement相同。

另外,正如greedybuddha指出的那样,请确保您的HashMap不会以同样的方式重新分配。

答案 1 :(得分:0)

尝试设置这些参数:

-XX:PermSize
-XX:MaxPermSize