java - OutOfMemoryError -
i'm making timetable scheduler final year project. last 2 days, i'm getting outofmemoryexception. have read lot exception, , tried increase memory alloted through -xms , -xmx options. none of these seem work me.
i profiled project, , found maximum space consumed hashmap objects, , mysql connection. have used static connection follows
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; } }
this code class named masterdata, extended other classes access database
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(); } }
an example of class uses this
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 teacher 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 * teacher teacherid not in(select classteacher division 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 * teacher teachername='"+teachername+"';"); if(rs.next()) return true; }catch(exception e){e.printstacktrace();} return false; } public boolean insert(){ int t; try{ t = st.executeupdate("insert 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 teacher 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+"' teacherid="+teacherid+";"); if(t!=0) return true; } catch(exception e){ e.printstacktrace(); return false; } return false; } }
my intention create single static connection entire program. seems work well. possible cause of problem?
try set these parameters well:
-xx:permsize -xx:maxpermsize
Comments
Post a Comment